xref: /illumos-gate/usr/src/uts/sun4u/daktari/os/daktari.c (revision e498729e15e121c426eb82534224f2cce4b2e020)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/cpuvar.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sunddi.h>
33 #include <sys/ddi.h>
34 #include <sys/sysmacros.h>
35 #include <sys/note.h>
36 
37 #include <sys/modctl.h>		/* for modload() */
38 #include <sys/platform_module.h>
39 #include <sys/errno.h>
40 #include <sys/daktari.h>
41 #include <sys/machsystm.h>
42 #include <sys/promif.h>
43 #include <vm/page.h>
44 #include <sys/memnode.h>
45 #include <vm/vm_dep.h>
46 
47 /* I2C Stuff */
48 #include <sys/i2c/clients/i2c_client.h>
49 
50 
51 int (*p2get_mem_unum)(int, uint64_t, char *, int, int *);
52 
53 /* Daktari Keyswitch Information */
54 #define	DAK_KEY_POLL_PORT	3
55 #define	DAK_KEY_POLL_BIT	2
56 #define	DAK_KEY_POLL_INTVL	10
57 
58 static	boolean_t	key_locked_bit;
59 static	clock_t		keypoll_timeout_hz;
60 
61 /*
62  * Table that maps memory slices to a specific memnode.
63  */
64 int slice_to_memnode[DAK_MAX_SLICE];
65 
66 /*
67  * For software memory interleaving support.
68  */
69 static	void update_mem_bounds(int, int, int, uint64_t, uint64_t);
70 
71 static uint64_t
72 slice_table[DAK_SBD_SLOTS][DAK_CPUS_PER_BOARD][DAK_BANKS_PER_MC][2];
73 
74 #define	SLICE_PA	0
75 #define	SLICE_SPAN	1
76 
77 int (*daktari_ssc050_get_port_bit) (dev_info_t *, int, int, uint8_t *, int);
78 extern	void (*abort_seq_handler)();
79 static	int daktari_dev_search(dev_info_t *, void *);
80 static	void keyswitch_poll(void *);
81 static	void daktari_abort_seq_handler(char *msg);
82 
83 void
84 startup_platform(void)
85 {
86 	/*
87 	 * Disable an active h/w watchdog timer
88 	 * upon exit to OBP.
89 	 */
90 	extern int disable_watchdog_on_exit;
91 	disable_watchdog_on_exit = 1;
92 }
93 
94 int
95 set_platform_tsb_spares()
96 {
97 	return (0);
98 }
99 
100 #pragma weak mmu_init_large_pages
101 
102 void
103 set_platform_defaults(void)
104 {
105 	extern int ts_dispatch_extended;
106 	extern uchar_t *ctx_pgsz_array;
107 	extern void mmu_init_large_pages(size_t);
108 
109 	/*
110 	 * Use the alternate TS dispatch table for USIII+ forward,
111 	 * which is better tuned for large servers.
112 	 */
113 	if ((ts_dispatch_extended == -1) && (ctx_pgsz_array != NULL))
114 		ts_dispatch_extended = 1;
115 
116 	if ((mmu_page_sizes == max_mmu_page_sizes) &&
117 	    (mmu_ism_pagesize != MMU_PAGESIZE32M)) {
118 		if (&mmu_init_large_pages)
119 			mmu_init_large_pages(mmu_ism_pagesize);
120 	}
121 }
122 
123 void
124 load_platform_modules(void)
125 {
126 	if (modload("misc", "pcihp") < 0) {
127 		cmn_err(CE_NOTE, "pcihp driver failed to load");
128 	}
129 	if (modload("drv", "pmc") < 0) {
130 		cmn_err(CE_NOTE, "pmc driver failed to load");
131 	}
132 
133 }
134 
135 void
136 load_platform_drivers(void)
137 {
138 	char **drv;
139 	dev_info_t	*keysw_dip;
140 
141 	static char *boot_time_drivers[] = {
142 		"hpc3130",
143 		"todds1287",
144 		"mc-us3",
145 		"ssc050",
146 		"pcisch",
147 		NULL
148 	};
149 
150 	for (drv = boot_time_drivers; *drv; drv++) {
151 		if (i_ddi_attach_hw_nodes(*drv) != DDI_SUCCESS)
152 			cmn_err(CE_WARN, "Failed to install \"%s\" driver.",
153 			    *drv);
154 	}
155 
156 	/*
157 	 * mc-us3 & ssc050 must stay loaded for plat_get_mem_unum()
158 	 * and keyswitch_poll()
159 	 */
160 	(void) ddi_hold_driver(ddi_name_to_major("mc-us3"));
161 	(void) ddi_hold_driver(ddi_name_to_major("ssc050"));
162 
163 	/* Gain access into the ssc050_get_port function */
164 	daktari_ssc050_get_port_bit = (int (*) (dev_info_t *, int, int,
165 		uint8_t *, int)) modgetsymvalue("ssc050_get_port_bit", 0);
166 	if (daktari_ssc050_get_port_bit == NULL) {
167 		cmn_err(CE_WARN, "cannot find ssc050_get_port_bit");
168 		return;
169 	}
170 
171 	ddi_walk_devs(ddi_root_node(), daktari_dev_search, (void *)&keysw_dip);
172 	ASSERT(keysw_dip != NULL);
173 
174 	keypoll_timeout_hz = drv_usectohz(10 * MICROSEC);
175 	keyswitch_poll(keysw_dip);
176 	abort_seq_handler = daktari_abort_seq_handler;
177 }
178 
179 static int
180 daktari_dev_search(dev_info_t *dip, void *arg)
181 {
182 	char		*compatible = NULL; /* Search tree for "i2c-ssc050" */
183 	int		*dev_regs; /* Info about where the device is. */
184 	uint_t		len;
185 	int		err;
186 
187 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
188 				"compatible", &compatible) != DDI_PROP_SUCCESS)
189 		return (DDI_WALK_CONTINUE);
190 
191 	if (strcmp(compatible, "i2c-ssc050") == 0) {
192 		ddi_prop_free(compatible);
193 
194 		err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
195 			DDI_PROP_DONTPASS, "reg", &dev_regs, &len);
196 		if (err != DDI_PROP_SUCCESS) {
197 			return (DDI_WALK_CONTINUE);
198 		}
199 		/*
200 		 * regs[0] contains the bus number and regs[1]
201 		 * contains the device address of the i2c device.
202 		 * 0x82 is the device address of the i2c device
203 		 * from which  the key switch position is read.
204 		 */
205 		if (dev_regs[0] == 0 && dev_regs[1] == 0x82) {
206 			*((dev_info_t **)arg) = dip;
207 			ddi_prop_free(dev_regs);
208 			return (DDI_WALK_TERMINATE);
209 		}
210 		ddi_prop_free(dev_regs);
211 	} else {
212 		ddi_prop_free(compatible);
213 	}
214 	return (DDI_WALK_CONTINUE);
215 }
216 
217 static void
218 keyswitch_poll(void *arg)
219 {
220 	dev_info_t	*dip = arg;
221 	uchar_t	port_byte;
222 	int	port = DAK_KEY_POLL_PORT;
223 	int	bit = DAK_KEY_POLL_BIT;
224 	int	err;
225 
226 	err = daktari_ssc050_get_port_bit(dip, port, bit,
227 		&port_byte, I2C_NOSLEEP);
228 	if (err != 0) {
229 		return;
230 	}
231 
232 	key_locked_bit = (boolean_t)((port_byte & 0x1));
233 	timeout(keyswitch_poll, (caddr_t)dip, keypoll_timeout_hz);
234 }
235 
236 static void
237 daktari_abort_seq_handler(char *msg)
238 {
239 	if (key_locked_bit == 0)
240 		cmn_err(CE_CONT, "KEY in LOCKED position, "
241 			"ignoring debug enter sequence");
242 	else  {
243 		debug_enter(msg);
244 	}
245 }
246 
247 
248 int
249 plat_cpu_poweron(struct cpu *cp)
250 {
251 	_NOTE(ARGUNUSED(cp))
252 	return (ENOTSUP);
253 }
254 
255 int
256 plat_cpu_poweroff(struct cpu *cp)
257 {
258 	_NOTE(ARGUNUSED(cp))
259 	return (ENOTSUP);
260 }
261 
262 /*
263  * Given a pfn, return the board and beginning/end of the page's
264  * memory controller's address range.
265  */
266 static int
267 plat_discover_slice(pfn_t pfn, pfn_t *first, pfn_t *last)
268 {
269 	int bd, cpu, bank;
270 
271 	for (bd = 0; bd < DAK_SBD_SLOTS; bd++) {
272 		for (cpu = 0; cpu < DAK_CPUS_PER_BOARD; cpu++) {
273 			for (bank = 0; bank < DAK_BANKS_PER_MC; bank++) {
274 				uint64_t *slice = slice_table[bd][cpu][bank];
275 				uint64_t base = btop(slice[SLICE_PA]);
276 				uint64_t len = btop(slice[SLICE_SPAN]);
277 				if (len && pfn >= base && pfn < (base + len)) {
278 					*first = base;
279 					*last = base + len - 1;
280 					return (bd);
281 				}
282 			}
283 		}
284 	}
285 	panic("plat_discover_slice: no slice for pfn 0x%lx\n", pfn);
286 	/* NOTREACHED */
287 }
288 
289 /*ARGSUSED*/
290 void
291 plat_freelist_process(int mnode)
292 {}
293 
294 
295 /*
296  * Called for each board/cpu/PA range detected in plat_fill_mc().
297  */
298 static void
299 update_mem_bounds(int boardid, int cpuid, int bankid,
300 	uint64_t base, uint64_t size)
301 {
302 	uint64_t	end;
303 	int		mnode;
304 
305 	slice_table[boardid][cpuid][bankid][SLICE_PA] = base;
306 	slice_table[boardid][cpuid][bankid][SLICE_SPAN] = size;
307 
308 	end = base + size - 1;
309 
310 	/*
311 	 * First see if this board already has a memnode associated
312 	 * with it.  If not, see if this slice has a memnode.  This
313 	 * covers the cases where a single slice covers multiple
314 	 * boards (cross-board interleaving) and where a single
315 	 * board has multiple slices (1+GB DIMMs).
316 	 */
317 	if ((mnode = plat_lgrphand_to_mem_node(boardid)) == -1) {
318 		if ((mnode = slice_to_memnode[PA_2_SLICE(base)]) == -1)
319 			mnode = mem_node_alloc();
320 
321 		ASSERT(mnode >= 0);
322 		ASSERT(mnode < MAX_MEM_NODES);
323 		plat_assign_lgrphand_to_mem_node(boardid, mnode);
324 	}
325 
326 	base = P2ALIGN(base, (1ul << PA_SLICE_SHIFT));
327 
328 	while (base < end) {
329 		slice_to_memnode[PA_2_SLICE(base)] = mnode;
330 		base += (1ul << PA_SLICE_SHIFT);
331 	}
332 }
333 
334 /*
335  * Dynamically detect memory slices in the system by decoding
336  * the cpu memory decoder registers at boot time.
337  */
338 void
339 plat_fill_mc(dnode_t nodeid)
340 {
341 	uint64_t	mc_addr, saf_addr;
342 	uint64_t	mc_decode[DAK_BANKS_PER_MC];
343 	uint64_t	base, size;
344 	uint64_t	saf_mask;
345 	uint64_t	offset;
346 	uint32_t	regs[4];
347 	int		len;
348 	int		local_mc;
349 	int		portid;
350 	int		boardid;
351 	int		cpuid;
352 	int		i;
353 
354 	if ((prom_getprop(nodeid, "portid", (caddr_t)&portid) < 0) ||
355 	    (portid == -1))
356 		return;
357 
358 	/*
359 	 * Decode the board number from the MC portid.  Assumes
360 	 * portid == safari agentid.
361 	 */
362 	boardid = DAK_GETSLOT(portid);
363 	cpuid = DAK_GETSID(portid);
364 
365 	/*
366 	 * The "reg" property returns 4 32-bit values. The first two are
367 	 * combined to form a 64-bit address.  The second two are for a
368 	 * 64-bit size, but we don't actually need to look at that value.
369 	 */
370 	len = prom_getproplen(nodeid, "reg");
371 	if (len != (sizeof (uint32_t) * 4)) {
372 		prom_printf("Warning: malformed 'reg' property\n");
373 		return;
374 	}
375 	if (prom_getprop(nodeid, "reg", (caddr_t)regs) < 0)
376 		return;
377 	mc_addr = ((uint64_t)regs[0]) << 32;
378 	mc_addr |= (uint64_t)regs[1];
379 
380 	/*
381 	 * Figure out whether the memory controller we are examining
382 	 * belongs to this CPU or a different one.
383 	 */
384 	saf_addr = lddsafaddr(8);
385 	saf_mask = (uint64_t)SAF_MASK;
386 	if ((mc_addr & saf_mask) == saf_addr)
387 		local_mc = 1;
388 	else
389 		local_mc = 0;
390 
391 	for (i = 0; i < DAK_BANKS_PER_MC; i++) {
392 		/*
393 		 * Memory decode masks are at offsets 0x10 - 0x28.
394 		 */
395 		offset = 0x10 + (i << 3);
396 
397 		/*
398 		 * If the memory controller is local to this CPU, we use
399 		 * the special ASI to read the decode registers.
400 		 * Otherwise, we load the values from a magic address in
401 		 * I/O space.
402 		 */
403 		if (local_mc)
404 			mc_decode[i] = lddmcdecode(offset);
405 		else
406 			mc_decode[i] = lddphysio(mc_addr | offset);
407 
408 		/*
409 		 * If the upper bit is set, we have a valid mask
410 		 */
411 		if ((int64_t)mc_decode[i] < 0) {
412 			/*
413 			 * The memory decode register is a bitmask field,
414 			 * so we can decode that into both a base and
415 			 * a span.
416 			 */
417 			base = MC_BASE(mc_decode[i]) << PHYS2UM_SHIFT;
418 			size = MC_UK2SPAN(mc_decode[i]);
419 			update_mem_bounds(boardid, cpuid, i, base, size);
420 		}
421 	}
422 }
423 
424 
425 /*
426  * This routine is run midway through the boot process.  By the time we get
427  * here, we know about all the active CPU boards in the system, and we have
428  * extracted information about each board's memory from the memory
429  * controllers.  We have also figured out which ranges of memory will be
430  * assigned to which memnodes, so we walk the slice table to build the table
431  * of memnodes.
432  */
433 /* ARGSUSED */
434 void
435 plat_build_mem_nodes(u_longlong_t *list, size_t  nelems)
436 {
437 	int	slice;
438 	pfn_t   basepfn;
439 	pgcnt_t npgs;
440 
441 	mem_node_pfn_shift = PFN_SLICE_SHIFT;
442 	mem_node_physalign = (1ull << PA_SLICE_SHIFT);
443 	npgs = 1ull << PFN_SLICE_SHIFT;
444 
445 	for (slice = 0; slice < DAK_MAX_SLICE; slice++) {
446 		if (slice_to_memnode[slice] == -1)
447 			continue;
448 		basepfn = (uint64_t)slice << PFN_SLICE_SHIFT;
449 		mem_node_add_slice(basepfn, basepfn + npgs - 1);
450 	}
451 }
452 
453 
454 
455 /*
456  * Daktari support for lgroups.
457  *
458  * On Daktari, an lgroup platform handle == slot number.
459  *
460  * Mappings between lgroup handles and memnodes are managed
461  * in addition to mappings between memory slices and memnodes
462  * to support cross-board interleaving as well as multiple
463  * slices per board (e.g. >1GB DIMMs). The initial mapping
464  * of memnodes to lgroup handles is determined at boot time.
465  */
466 int
467 plat_pfn_to_mem_node(pfn_t pfn)
468 {
469 	return (slice_to_memnode[PFN_2_SLICE(pfn)]);
470 }
471 
472 /*
473  * Return the platform handle for the lgroup containing the given CPU
474  *
475  * For Daktari, lgroup platform handle == slot number
476  */
477 lgrp_handle_t
478 plat_lgrp_cpu_to_hand(processorid_t id)
479 {
480 	return (DAK_GETSLOT(id));
481 }
482 
483 /*
484  * Platform specific lgroup initialization
485  */
486 void
487 plat_lgrp_init(void)
488 {
489 	int i;
490 
491 	/*
492 	 * Initialize lookup tables to invalid values so we catch
493 	 * any illegal use of them.
494 	 */
495 	for (i = 0; i < DAK_MAX_SLICE; i++) {
496 		slice_to_memnode[i] = -1;
497 	}
498 }
499 
500 /*
501  * Return latency between "from" and "to" lgroups
502  *
503  * This latency number can only be used for relative comparison
504  * between lgroups on the running system, cannot be used across platforms,
505  * and may not reflect the actual latency.  It is platform and implementation
506  * specific, so platform gets to decide its value.  It would be nice if the
507  * number was at least proportional to make comparisons more meaningful though.
508  * NOTE: The numbers below are supposed to be load latencies for uncached
509  * memory divided by 10.
510  */
511 int
512 plat_lgrp_latency(lgrp_handle_t from, lgrp_handle_t to)
513 {
514 	/*
515 	 * Return min remote latency when there are more than two lgroups
516 	 * (root and child) and getting latency between two different lgroups
517 	 * or root is involved
518 	 */
519 	if (lgrp_optimizations() && (from != to ||
520 	    from == LGRP_DEFAULT_HANDLE || to == LGRP_DEFAULT_HANDLE))
521 		return (21);
522 	else
523 		return (19);
524 }
525 /*
526  * No platform drivers on this platform
527  */
528 char *platform_module_list[] = {
529 	(char *)0
530 };
531 
532 /*ARGSUSED*/
533 void
534 plat_tod_fault(enum tod_fault_type tod_bad)
535 {
536 }
537 
538 /*ARGSUSED*/
539 int
540 plat_get_mem_unum(int synd_code, uint64_t flt_addr, int flt_bus_id,
541     int flt_in_memory, ushort_t flt_status, char *buf, int buflen, int *lenp)
542 {
543 	if (flt_in_memory && (p2get_mem_unum != NULL))
544 		return (p2get_mem_unum(synd_code, P2ALIGN(flt_addr, 8),
545 			buf, buflen, lenp));
546 	else
547 		return (ENOTSUP);
548 }
549 
550 /*
551  * This platform hook gets called from mc_add_mem_unum_label() in the mc-us3
552  * driver giving each platform the opportunity to add platform
553  * specific label information to the unum for ECC error logging purposes.
554  */
555 void
556 plat_add_mem_unum_label(char *unum, int mcid, int bank, int dimm)
557 {
558 	_NOTE(ARGUNUSED(bank, dimm))
559 
560 	char board = DAK_GETSLOT_LABEL(mcid);
561 	char old_unum[UNUM_NAMLEN];
562 
563 	strcpy(old_unum, unum);
564 	snprintf(unum, UNUM_NAMLEN, "Slot %c: %s", board, old_unum);
565 }
566 
567 int
568 plat_get_cpu_unum(int cpuid, char *buf, int buflen, int *lenp)
569 {
570 	char board = DAK_GETSLOT_LABEL(cpuid);
571 
572 	if (snprintf(buf, buflen, "Slot %c", board) >= buflen) {
573 		return (ENOSPC);
574 	} else {
575 		*lenp = strlen(buf);
576 		return (0);
577 	}
578 }
579 
580 /*
581  * The zuluvm module requires a dmv interrupt for each installed zulu board.
582  */
583 void
584 plat_dmv_params(uint_t *hwint, uint_t *swint)
585 {
586 	*hwint = 0;
587 	*swint = DAK_SBD_SLOTS - 1;
588 }
589