]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/broadcom/bcm_machdep.c
dts: Import DTS from Linux 5.6
[FreeBSD/FreeBSD.git] / sys / mips / broadcom / bcm_machdep.c
1 /*-
2  * Copyright (c) 2007 Bruce M. Simpson.
3  * Copyright (c) 2016 Michael Zhilin <mizhka@gmail.com>
4  * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
5  * Copyright (c) 2017 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Landon Fuller
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "opt_ddb.h"
37
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/imgact.h>
43 #include <sys/bio.h>
44 #include <sys/buf.h>
45 #include <sys/bus.h>
46 #include <sys/cpu.h>
47 #include <sys/cons.h>
48 #include <sys/exec.h>
49 #include <sys/ucontext.h>
50 #include <sys/proc.h>
51 #include <sys/kdb.h>
52 #include <sys/ptrace.h>
53 #include <sys/reboot.h>
54 #include <sys/signalvar.h>
55 #include <sys/sysent.h>
56 #include <sys/sysproto.h>
57 #include <sys/user.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_param.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_page.h>
63 #include <vm/vm_phys.h>
64
65 #include <machine/cache.h>
66 #include <machine/clock.h>
67 #include <machine/cpu.h>
68 #include <machine/cpuinfo.h>
69 #include <machine/cpufunc.h>
70 #include <machine/cpuregs.h>
71 #include <machine/hwfunc.h>
72 #include <machine/intr_machdep.h>
73 #include <machine/locore.h>
74 #include <machine/md_var.h>
75 #include <machine/pte.h>
76 #include <machine/sigframe.h>
77 #include <machine/trap.h>
78
79 #include <dev/bhnd/bhnd.h>
80 #include <dev/bhnd/bhndreg.h>
81 #include <dev/bhnd/bhnd_eromvar.h>
82
83 #include <dev/bhnd/bcma/bcma_eromvar.h>
84
85 #include <dev/bhnd/siba/sibareg.h>
86 #include <dev/bhnd/siba/sibavar.h>
87
88 #include <dev/bhnd/cores/chipc/chipcreg.h>
89 #include <dev/bhnd/cores/pmu/bhnd_pmureg.h>
90
91 #include "bcm_machdep.h"
92 #include "bcm_bmips_exts.h"
93
94 #ifdef CFE
95 #include <dev/cfe/cfe_api.h>
96 #include <dev/cfe/cfe_error.h>
97 #endif
98
99 #if 0
100 #define BCM_TRACE(_fmt, ...)    printf(_fmt, ##__VA_ARGS__)
101 #else
102 #define BCM_TRACE(_fmt, ...)
103 #endif
104
105 static int      bcm_init_platform_data(struct bcm_platform *bp);
106
107 static int      bcm_find_core(struct bcm_platform *bp,
108                     const struct bhnd_core_match *descs, size_t num_descs,
109                     struct bhnd_core_info *info, uintptr_t *addr);
110
111 static int      bcm_erom_probe_and_attach(bhnd_erom_class_t **erom_cls,
112                     kobj_ops_t erom_ops, bhnd_erom_t *erom, size_t esize,
113                     struct bhnd_erom_io *eio, struct bhnd_chipid *cid);
114
115 extern int      *edata;
116 extern int      *end;
117
118 static struct bcm_platform       bcm_platform_data;
119 static bool                      bcm_platform_data_avail = false;
120
121 #ifdef CFE
122 static struct bcm_nvram_iocfe    bcm_cfe_nvram;
123 #endif
124
125 static const struct bhnd_core_match bcm_chipc_cores[] = {
126         { BHND_MATCH_CORE(BHND_MFGID_BCM,       BHND_COREID_CC)         },
127         { BHND_MATCH_CORE(BHND_MFGID_BCM,       BHND_COREID_4706_CC)    },
128 };
129
130 static const struct bhnd_core_match bcm_cpu0_cores[] = {
131         {
132                 BHND_MATCH_CORE_CLASS(BHND_DEVCLASS_CPU),
133                 BHND_MATCH_CORE_UNIT(0)
134         }
135 };
136
137 static const struct bhnd_core_match bcm_pmu_cores[] = {
138         { BHND_MATCH_CORE(BHND_MFGID_BCM,       BHND_COREID_PMU)        },
139 };
140
141 struct bcm_platform *
142 bcm_get_platform(void)
143 {
144         if (!bcm_platform_data_avail)
145                 panic("platform data not available");
146
147         return (&bcm_platform_data);
148 }
149
150 static bus_addr_t
151 bcm_get_bus_addr(void)
152 {
153         long maddr;
154
155         if (resource_long_value("bhnd", 0, "maddr", &maddr) == 0)
156                 return ((u_long)maddr);
157
158         return (BHND_DEFAULT_CHIPC_ADDR);
159 }
160
161 static bus_size_t
162 bcm_get_bus_size(void)
163 {
164         long msize;
165
166         if (resource_long_value("bhnd", 0, "msize", &msize) == 0)
167                 return ((u_long)msize);
168
169         return (BHND_DEFAULT_ENUM_SIZE);
170 }
171
172 /**
173  * Search the device enumeration table for a core matching @p descs,
174  * 
175  * @param       bp              Platform state containing a valid EROM parser.
176  * @param       descs           The core match descriptor table.
177  * @param       num_descs       The number of match descriptors in @p descs.
178  * @param[out]  info            If non-NULL, will be populated with the core
179  *                              info.
180  * @param[out]  addr            If non-NULL, will be populated with the core's
181  *                              physical register address.
182  */
183 static int
184 bcm_find_core(struct bcm_platform *bp, const struct bhnd_core_match *descs,
185     size_t num_descs, struct bhnd_core_info *info, uintptr_t *addr)
186 {
187         bhnd_addr_t             b_addr;
188         bhnd_size_t             b_size;
189         int                     error;
190
191         /* Fetch core info */
192         for (size_t i = 0; i < num_descs; i++) {
193                 error = bhnd_erom_lookup_core_addr(&bp->erom.obj, &descs[i],
194                     BHND_PORT_DEVICE, 0, 0, info, &b_addr, &b_size);
195
196                 /* Terminate search on first match */
197                 if (error == 0)
198                         break;
199
200                 /* Terminate on first error (other than core not found) */
201                 if (error != ENOENT)
202                         return (error);
203
204                 /* Continue search ... */
205         }
206
207         /* Provide the core's base address */
208         if (addr != NULL && b_addr > UINTPTR_MAX) {
209                 BCM_ERR("core address %#jx overflows native address width\n",
210                     (uintmax_t)b_addr);
211                 return (ERANGE);
212         }
213
214         if (addr != NULL)
215                 *addr = b_addr;
216
217         return (0);
218 }
219
220 /**
221  * Read a variable directly from NVRAM, decoding as @p type.
222  *
223  * @param               bp      Platform state.
224  * @param               name    The raw name of the variable to be fetched,
225  *                              including any device path (/pci/1/1/varname) or
226  *                              alias prefix (0:varname).
227  * @param[out]          buf     On success, the requested value will be written
228  *                              to this buffer. This argment may be NULL if
229  *                              the value is not desired.
230  * @param[in,out]       len     The capacity of @p buf. On success, will be set
231  *                              to the actual size of the requested value.
232  * @param               type    The data type to be written to @p buf.
233  *
234  * @retval 0            success
235  * @retval ENOMEM       If @p buf is non-NULL and a buffer of @p len is too
236  *                      small to hold the requested value.
237  * @retval ENOENT       If @p name is not found.
238  * @retval EFTYPE       If the variable data cannot be coerced to @p type.
239  * @retval ERANGE       If value coercion would overflow @p type.
240  * @retval non-zero     If parsing NVRAM otherwise fails, a regular unix error
241  *                      code will be returned.
242  */
243 int
244 bcm_get_nvram(struct bcm_platform *bp, const char *name, void *buf, size_t *len,
245     bhnd_nvram_type type)
246 {
247         if (bp->nvram_io == NULL || bp->nvram_cls == NULL)
248                 return (ENOENT);
249
250         return (bhnd_nvram_data_getvar_direct(bp->nvram_cls, bp->nvram_io, name,
251             buf, len, type));
252 }
253
254 /**
255  * Probe and attach a bhnd_erom parser instance for the bhnd bus.
256  * 
257  * @param[out]  erom_cls        The probed EROM class.
258  * @param[out]  erom_ops        The storage to be used when compiling
259  *                              @p erom_cls.
260  * @param[out]  erom            The storage to be used when initializing the
261  *                              static instance of @p erom_cls.
262  * @param       esize           The total available number of bytes allocated
263  *                              for @p erom. If this is less than is required
264  *                              by @p erom_cls ENOMEM will be returned.
265  * @param       eio             EROM I/O callbacks to be used.
266  * @param[out]  cid             On success, the probed chip identification.
267  */
268 static int
269 bcm_erom_probe_and_attach(bhnd_erom_class_t **erom_cls, kobj_ops_t erom_ops,
270     bhnd_erom_t *erom, size_t esize, struct bhnd_erom_io *eio,
271     struct bhnd_chipid *cid)
272 {
273         bhnd_erom_class_t       **clsp;
274         bus_addr_t                bus_addr;
275         int                       error, prio, result;
276
277         *erom_cls = NULL;
278         prio = 0;
279
280         /* Map our first bus core for the erom probe */
281         bus_addr = bcm_get_bus_addr();
282         if ((error = bhnd_erom_io_map(eio, bus_addr, BHND_DEFAULT_CORE_SIZE))) {
283                 BCM_ERR("failed to map first core at %#jx+%#jx: %d\n",
284                     (uintmax_t)bus_addr, (uintmax_t)BHND_DEFAULT_CORE_SIZE,
285                     error);
286
287                 return (error);
288         }
289
290         SET_FOREACH(clsp, bhnd_erom_class_set) {
291                 struct bhnd_chipid       pcid;
292                 bhnd_erom_class_t       *cls;
293                 struct kobj_ops          kops;
294
295                 cls = *clsp;
296
297                 /* Compile the class' ops table */
298                 kobj_class_compile_static(cls, &kops);
299
300                 /* Probe the bus address */
301                 result = bhnd_erom_probe(cls, eio, NULL, &pcid);
302
303                 /* Drop pointer to stack allocated ops table */
304                 cls->ops = NULL;
305
306                 /* The parser did not match if an error was returned */
307                 if (result > 0)
308                         continue;
309
310                 /* Check for a new highest priority match */
311                 if (*erom_cls == NULL || result > prio) {
312                         prio = result;
313
314                         *cid = pcid;
315                         *erom_cls = cls;
316                 }
317
318                 /* Terminate immediately on BUS_PROBE_SPECIFIC */
319                 if (result == BUS_PROBE_SPECIFIC)
320                         break;
321         }
322
323         /* Valid EROM class probed? */
324         if (*erom_cls == NULL) {
325                 BCM_ERR("no erom parser found for root bus at %#jx\n", 
326                     (uintmax_t)bus_addr);
327
328                 return (ENOENT);
329         }
330
331         /* Using the provided storage, recompile the erom class ... */
332         kobj_class_compile_static(*erom_cls, erom_ops);
333
334         /* ... and initialize the erom parser instance */
335         error = bhnd_erom_init_static(*erom_cls, erom, esize, cid, eio);
336
337         return (error);
338 }
339
340 /**
341  * Populate platform configuration data.
342  */
343 static int
344 bcm_init_platform_data(struct bcm_platform *bp)
345 {
346         bus_addr_t              bus_addr, bus_size;
347         bus_space_tag_t         erom_bst;
348         bus_space_handle_t      erom_bsh;
349         bool                    aob, pmu;
350         int                     error;
351
352         bus_addr = bcm_get_bus_addr();
353         bus_size = bcm_get_bus_size();
354
355 #ifdef CFE
356         /* Fetch CFE console handle (if any). Must be initialized before
357          * any calls to printf/early_putc. */
358         if ((bp->cfe_console = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE)) < 0)
359                 bp->cfe_console = -1;
360
361         /* Probe CFE NVRAM sources */
362         bp->nvram_io = &bcm_cfe_nvram.io;
363         error = bcm_nvram_find_cfedev(&bcm_cfe_nvram, &bp->nvram_cls);
364         if (error) {
365                 bp->nvram_io = NULL;
366                 bp->nvram_cls = NULL;
367         }
368 #endif /* CFE */
369
370         /* Probe and attach device table provider, populating our
371          * chip identification */
372         erom_bst = mips_bus_space_generic;
373         erom_bsh = BCM_SOC_BSH(bus_addr, 0);
374
375         error = bhnd_erom_iobus_init(&bp->erom_io, bus_addr, bus_size, erom_bst,
376             erom_bsh);
377         if (error) {
378                 BCM_ERR("failed to initialize erom I/O callbacks: %d\n", error);
379                 return (error);
380         }
381
382         error = bcm_erom_probe_and_attach(&bp->erom_impl, &bp->erom_ops,
383             &bp->erom.obj, sizeof(bp->erom), &bp->erom_io.eio, &bp->cid);
384         if (error) {
385                 BCM_ERR("error attaching erom parser: %d\n", error);
386                 bhnd_erom_io_fini(&bp->erom_io.eio);
387                 return (error);
388         }
389
390         if (bootverbose)
391                 bhnd_erom_dump(&bp->erom.obj);
392
393         /* Fetch chipcommon core info */
394         error = bcm_find_core(bp, bcm_chipc_cores, nitems(bcm_chipc_cores),
395             &bp->cc_id, &bp->cc_addr);
396         if (error) {
397                 BCM_ERR("error locating chipc core: %d\n", error);
398                 return (error);
399         }
400
401         /* Fetch chipc capability flags */
402         bp->cc_caps = BCM_SOC_READ_4(bp->cc_addr, CHIPC_CAPABILITIES);
403         bp->cc_caps_ext = 0x0;  
404
405         if (CHIPC_HWREV_HAS_CAP_EXT(bp->cc_id.hwrev))
406                 bp->cc_caps_ext = BCM_CHIPC_READ_4(bp, CHIPC_CAPABILITIES_EXT);
407
408         /* Fetch PMU info */
409         pmu = CHIPC_GET_FLAG(bp->cc_caps, CHIPC_CAP_PMU);
410         aob = CHIPC_GET_FLAG(bp->cc_caps_ext, CHIPC_CAP2_AOB);
411
412         if (pmu && aob) {
413                 /* PMU block mapped to a PMU core on the Always-on-Bus (aob) */
414                 error = bcm_find_core(bp, bcm_pmu_cores, nitems(bcm_pmu_cores),
415                     &bp->pmu_id,  &bp->pmu_addr);
416                 if (error) {
417                         BCM_ERR("error locating pmu core: %d\n", error);
418                         return (error);
419                 }
420         } else if (pmu) {
421                 /* PMU block mapped to chipc */
422                 bp->pmu_addr = bp->cc_addr;
423                 bp->pmu_id = bp->cc_id;
424         } else {
425                 /* No PMU */
426                 bp->pmu_addr = 0x0;
427                 memset(&bp->pmu_id, 0, sizeof(bp->pmu_id));
428         }
429
430         /* Initialize PMU query state */
431         if (pmu) {
432                 error = bhnd_pmu_query_init(&bp->pmu, NULL, bp->cid,
433                     &bcm_pmu_soc_io, bp);
434                 if (error) {
435                         BCM_ERR("bhnd_pmu_query_init() failed: %d\n", error);
436                         return (error);
437                 }
438         }
439
440         /* Find CPU core info */
441         error = bcm_find_core(bp, bcm_cpu0_cores, nitems(bcm_cpu0_cores),
442             &bp->cpu_id,  &bp->cpu_addr);
443         if (error) {
444                 BCM_ERR("error locating CPU core: %d\n", error);
445                 return (error);
446         }
447
448         /* Initialize our platform service registry */
449         if ((error = bhnd_service_registry_init(&bp->services))) {
450                 BCM_ERR("error initializing service registry: %d\n", error);
451                 return (error);
452         }
453
454         bcm_platform_data_avail = true;
455         return (0);
456 }
457
458 void
459 platform_cpu_init()
460 {
461         /* Nothing special */
462 }
463
464 static void
465 mips_init(void)
466 {
467         int i, j;
468
469         printf("entry: mips_init()\n");
470
471 #ifdef CFE
472         /*
473          * Query DRAM memory map from CFE.
474          */
475         physmem = 0;
476         for (i = 0; i < 10; i += 2) {
477                 int result;
478                 uint64_t addr, len, type;
479
480                 result = cfe_enummem(i / 2, 0, &addr, &len, &type);
481                 if (result < 0) {
482                         BCM_TRACE("There is no phys memory for: %d\n", i);
483                         phys_avail[i] = phys_avail[i + 1] = 0;
484                         break;
485                 }
486                 if (type != CFE_MI_AVAILABLE) {
487                         BCM_TRACE("phys memory is not available: %d\n", i);
488                         continue;
489                 }
490
491                 phys_avail[i] = addr;
492                 if (i == 0 && addr == 0) {
493                         /*
494                          * If this is the first physical memory segment probed
495                          * from CFE, omit the region at the start of physical
496                          * memory where the kernel has been loaded.
497                          */
498                         phys_avail[i] += MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
499                 }
500                 
501                 BCM_TRACE("phys memory is available for: %d\n", i);
502                 BCM_TRACE(" => addr =  %jx\n", addr);
503                 BCM_TRACE(" => len =  %jd\n", len);
504
505                 phys_avail[i + 1] = addr + len;
506                 physmem += len;
507         }
508
509         BCM_TRACE("Total phys memory is : %ld\n", physmem);
510         realmem = btoc(physmem);
511 #endif
512
513         for (j = 0; j < i; j++)
514                 dump_avail[j] = phys_avail[j];
515
516         physmem = realmem;
517
518         init_param1();
519         init_param2(physmem);
520         mips_cpu_init();
521         pmap_bootstrap();
522         mips_proc0_init();
523         mutex_init();
524         kdb_init();
525 #ifdef KDB
526         if (boothowto & RB_KDB)
527                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
528 #endif
529 }
530
531 void
532 platform_reset(void)
533 {
534         struct bcm_platform     *bp;
535         bool                     bcm4785war;
536
537         printf("bcm::platform_reset()\n");
538         intr_disable();
539
540 #ifdef CFE
541         /* Fall back on CFE if reset requested during platform
542          * data initialization */
543         if (!bcm_platform_data_avail) {
544                 cfe_exit(0, 0);
545                 while (1);
546         }
547 #endif
548
549         bp = bcm_get_platform();
550         bcm4785war = false;
551
552         /* Handle BCM4785-specific behavior */
553         if (bp->cid.chip_id == BHND_CHIPID_BCM4785) {
554                 bcm4785war = true;
555
556                 /* Switch to async mode */
557                 bcm_bmips_wr_pllcfg3(BMIPS_BCMCFG_PLLCFG3_SM);
558         }
559
560         /* Set watchdog (PMU or ChipCommon) */
561         if (bp->pmu_addr != 0x0) {
562                 BCM_PMU_WRITE_4(bp, BHND_PMU_WATCHDOG, 1);
563         } else
564                 BCM_CHIPC_WRITE_4(bp, CHIPC_WATCHDOG, 1);
565
566         /* BCM4785 */
567         if (bcm4785war) {
568                 mips_sync();
569                 __asm __volatile("wait");
570         }
571
572         while (1);
573 }
574
575 void
576 platform_start(__register_t a0, __register_t a1, __register_t a2,
577                __register_t a3)
578 {
579         vm_offset_t              kernend;
580         uint64_t                 platform_counter_freq;
581         int                      error;
582
583         /* clear the BSS and SBSS segments */
584         kernend = (vm_offset_t)&end;
585         memset(&edata, 0, kernend - (vm_offset_t)(&edata));
586
587         mips_postboot_fixup();
588
589         /* Initialize pcpu stuff */
590         mips_pcpu0_init();
591
592 #ifdef CFE
593         /*
594          * Initialize CFE firmware trampolines. This must be done
595          * before any CFE APIs are called, including writing
596          * to the CFE console.
597          *
598          * CFE passes the following values in registers:
599          * a0: firmware handle
600          * a2: firmware entry point
601          * a3: entry point seal
602          */
603         if (a3 == CFE_EPTSEAL)
604                 cfe_init(a0, a2);
605 #endif
606
607         /* Init BCM platform data */
608         if ((error = bcm_init_platform_data(&bcm_platform_data)))
609                 panic("bcm_init_platform_data() failed: %d", error);
610
611         platform_counter_freq = bcm_get_cpufreq(bcm_get_platform());
612
613         /* CP0 ticks every two cycles */
614         mips_timer_early_init(platform_counter_freq / 2);
615
616         cninit();
617
618         mips_init();
619
620         mips_timer_init_params(platform_counter_freq, 1);
621 }
622
623 /*
624  * CFE-based EARLY_PRINTF support. To use, add the following to the kernel
625  * config:
626  *      option EARLY_PRINTF
627  *      option CFE
628  *      device cfe
629  */
630 #if defined(EARLY_PRINTF) && defined(CFE)
631 static void
632 bcm_cfe_eputc(int c)
633 {
634         unsigned char   ch;
635         int             handle;
636
637         ch = (unsigned char) c;
638
639         /* bcm_get_platform() cannot be used here, as we may be called
640          * from bcm_init_platform_data(). */
641         if ((handle = bcm_platform_data.cfe_console) < 0)
642                 return;
643
644         if (ch == '\n')
645                 early_putc('\r');
646
647         while ((cfe_write(handle, &ch, 1)) == 0)
648                 continue;
649 }
650
651 early_putc_t *early_putc = bcm_cfe_eputc;
652 #endif /* EARLY_PRINTF */