1 /*
2  *  PS3 repository routines.
3  *
4  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
5  *  Copyright 2006 Sony Corp.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; version 2 of the License.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #include <asm/lv1call.h>
22 
23 #include "platform.h"
24 
25 enum ps3_vendor_id {
26 	PS3_VENDOR_ID_NONE = 0,
27 	PS3_VENDOR_ID_SONY = 0x8000000000000000UL,
28 };
29 
30 enum ps3_lpar_id {
31 	PS3_LPAR_ID_CURRENT = 0,
32 	PS3_LPAR_ID_PME = 1,
33 };
34 
35 #define dump_field(_a, _b) _dump_field(_a, _b, __func__, __LINE__)
36 static void _dump_field(const char *hdr, u64 n, const char *func, int line)
37 {
38 #if defined(DEBUG)
39 	char s[16];
40 	const char *const in = (const char *)&n;
41 	unsigned int i;
42 
43 	for (i = 0; i < 8; i++)
44 		s[i] = (in[i] <= 126 && in[i] >= 32) ? in[i] : '.';
45 	s[i] = 0;
46 
47 	pr_devel("%s:%d: %s%016llx : %s\n", func, line, hdr, n, s);
48 #endif
49 }
50 
51 #define dump_node_name(_a, _b, _c, _d, _e) \
52 	_dump_node_name(_a, _b, _c, _d, _e, __func__, __LINE__)
53 static void _dump_node_name(unsigned int lpar_id, u64 n1, u64 n2, u64 n3,
54 	u64 n4, const char *func, int line)
55 {
56 	pr_devel("%s:%d: lpar: %u\n", func, line, lpar_id);
57 	_dump_field("n1: ", n1, func, line);
58 	_dump_field("n2: ", n2, func, line);
59 	_dump_field("n3: ", n3, func, line);
60 	_dump_field("n4: ", n4, func, line);
61 }
62 
63 #define dump_node(_a, _b, _c, _d, _e, _f, _g) \
64 	_dump_node(_a, _b, _c, _d, _e, _f, _g, __func__, __LINE__)
65 static void _dump_node(unsigned int lpar_id, u64 n1, u64 n2, u64 n3, u64 n4,
66 	u64 v1, u64 v2, const char *func, int line)
67 {
68 	pr_devel("%s:%d: lpar: %u\n", func, line, lpar_id);
69 	_dump_field("n1: ", n1, func, line);
70 	_dump_field("n2: ", n2, func, line);
71 	_dump_field("n3: ", n3, func, line);
72 	_dump_field("n4: ", n4, func, line);
73 	pr_devel("%s:%d: v1: %016llx\n", func, line, v1);
74 	pr_devel("%s:%d: v2: %016llx\n", func, line, v2);
75 }
76 
77 /**
78  * make_first_field - Make the first field of a repository node name.
79  * @text: Text portion of the field.
80  * @index: Numeric index portion of the field.  Use zero for 'don't care'.
81  *
82  * This routine sets the vendor id to zero (non-vendor specific).
83  * Returns field value.
84  */
85 
86 static u64 make_first_field(const char *text, u64 index)
87 {
88 	u64 n;
89 
90 	strncpy((char *)&n, text, 8);
91 	return PS3_VENDOR_ID_NONE + (n >> 32) + index;
92 }
93 
94 /**
95  * make_field - Make subsequent fields of a repository node name.
96  * @text: Text portion of the field.  Use "" for 'don't care'.
97  * @index: Numeric index portion of the field.  Use zero for 'don't care'.
98  *
99  * Returns field value.
100  */
101 
102 static u64 make_field(const char *text, u64 index)
103 {
104 	u64 n;
105 
106 	strncpy((char *)&n, text, 8);
107 	return n + index;
108 }
109 
110 /**
111  * read_node - Read a repository node from raw fields.
112  * @n1: First field of node name.
113  * @n2: Second field of node name.  Use zero for 'don't care'.
114  * @n3: Third field of node name.  Use zero for 'don't care'.
115  * @n4: Fourth field of node name.  Use zero for 'don't care'.
116  * @v1: First repository value (high word).
117  * @v2: Second repository value (low word).  Optional parameter, use zero
118  *      for 'don't care'.
119  */
120 
121 static int read_node(unsigned int lpar_id, u64 n1, u64 n2, u64 n3, u64 n4,
122 	u64 *_v1, u64 *_v2)
123 {
124 	int result;
125 	u64 v1;
126 	u64 v2;
127 
128 	if (lpar_id == PS3_LPAR_ID_CURRENT) {
129 		u64 id;
130 		lv1_get_logical_partition_id(&id);
131 		lpar_id = id;
132 	}
133 
134 	result = lv1_read_repository_node(lpar_id, n1, n2, n3, n4, &v1,
135 		&v2);
136 
137 	if (result) {
138 		pr_warn("%s:%d: lv1_read_repository_node failed: %s\n",
139 			__func__, __LINE__, ps3_result(result));
140 		dump_node_name(lpar_id, n1, n2, n3, n4);
141 		return -ENOENT;
142 	}
143 
144 	dump_node(lpar_id, n1, n2, n3, n4, v1, v2);
145 
146 	if (_v1)
147 		*_v1 = v1;
148 	if (_v2)
149 		*_v2 = v2;
150 
151 	if (v1 && !_v1)
152 		pr_devel("%s:%d: warning: discarding non-zero v1: %016llx\n",
153 			__func__, __LINE__, v1);
154 	if (v2 && !_v2)
155 		pr_devel("%s:%d: warning: discarding non-zero v2: %016llx\n",
156 			__func__, __LINE__, v2);
157 
158 	return 0;
159 }
160 
161 int ps3_repository_read_bus_str(unsigned int bus_index, const char *bus_str,
162 	u64 *value)
163 {
164 	return read_node(PS3_LPAR_ID_PME,
165 		make_first_field("bus", bus_index),
166 		make_field(bus_str, 0),
167 		0, 0,
168 		value, NULL);
169 }
170 
171 int ps3_repository_read_bus_id(unsigned int bus_index, u64 *bus_id)
172 {
173 	return read_node(PS3_LPAR_ID_PME, make_first_field("bus", bus_index),
174 			 make_field("id", 0), 0, 0, bus_id, NULL);
175 }
176 
177 int ps3_repository_read_bus_type(unsigned int bus_index,
178 	enum ps3_bus_type *bus_type)
179 {
180 	int result;
181 	u64 v1 = 0;
182 
183 	result = read_node(PS3_LPAR_ID_PME,
184 		make_first_field("bus", bus_index),
185 		make_field("type", 0),
186 		0, 0,
187 		&v1, NULL);
188 	*bus_type = v1;
189 	return result;
190 }
191 
192 int ps3_repository_read_bus_num_dev(unsigned int bus_index,
193 	unsigned int *num_dev)
194 {
195 	int result;
196 	u64 v1 = 0;
197 
198 	result = read_node(PS3_LPAR_ID_PME,
199 		make_first_field("bus", bus_index),
200 		make_field("num_dev", 0),
201 		0, 0,
202 		&v1, NULL);
203 	*num_dev = v1;
204 	return result;
205 }
206 
207 int ps3_repository_read_dev_str(unsigned int bus_index,
208 	unsigned int dev_index, const char *dev_str, u64 *value)
209 {
210 	return read_node(PS3_LPAR_ID_PME,
211 		make_first_field("bus", bus_index),
212 		make_field("dev", dev_index),
213 		make_field(dev_str, 0),
214 		0,
215 		value, NULL);
216 }
217 
218 int ps3_repository_read_dev_id(unsigned int bus_index, unsigned int dev_index,
219 	u64 *dev_id)
220 {
221 	return read_node(PS3_LPAR_ID_PME, make_first_field("bus", bus_index),
222 			 make_field("dev", dev_index), make_field("id", 0), 0,
223 			 dev_id, NULL);
224 }
225 
226 int ps3_repository_read_dev_type(unsigned int bus_index,
227 	unsigned int dev_index, enum ps3_dev_type *dev_type)
228 {
229 	int result;
230 	u64 v1 = 0;
231 
232 	result = read_node(PS3_LPAR_ID_PME,
233 		make_first_field("bus", bus_index),
234 		make_field("dev", dev_index),
235 		make_field("type", 0),
236 		0,
237 		&v1, NULL);
238 	*dev_type = v1;
239 	return result;
240 }
241 
242 int ps3_repository_read_dev_intr(unsigned int bus_index,
243 	unsigned int dev_index, unsigned int intr_index,
244 	enum ps3_interrupt_type *intr_type, unsigned int *interrupt_id)
245 {
246 	int result;
247 	u64 v1 = 0;
248 	u64 v2 = 0;
249 
250 	result = read_node(PS3_LPAR_ID_PME,
251 		make_first_field("bus", bus_index),
252 		make_field("dev", dev_index),
253 		make_field("intr", intr_index),
254 		0,
255 		&v1, &v2);
256 	*intr_type = v1;
257 	*interrupt_id = v2;
258 	return result;
259 }
260 
261 int ps3_repository_read_dev_reg_type(unsigned int bus_index,
262 	unsigned int dev_index, unsigned int reg_index,
263 	enum ps3_reg_type *reg_type)
264 {
265 	int result;
266 	u64 v1 = 0;
267 
268 	result = read_node(PS3_LPAR_ID_PME,
269 		make_first_field("bus", bus_index),
270 		make_field("dev", dev_index),
271 		make_field("reg", reg_index),
272 		make_field("type", 0),
273 		&v1, NULL);
274 	*reg_type = v1;
275 	return result;
276 }
277 
278 int ps3_repository_read_dev_reg_addr(unsigned int bus_index,
279 	unsigned int dev_index, unsigned int reg_index, u64 *bus_addr, u64 *len)
280 {
281 	return read_node(PS3_LPAR_ID_PME,
282 		make_first_field("bus", bus_index),
283 		make_field("dev", dev_index),
284 		make_field("reg", reg_index),
285 		make_field("data", 0),
286 		bus_addr, len);
287 }
288 
289 int ps3_repository_read_dev_reg(unsigned int bus_index,
290 	unsigned int dev_index, unsigned int reg_index,
291 	enum ps3_reg_type *reg_type, u64 *bus_addr, u64 *len)
292 {
293 	int result = ps3_repository_read_dev_reg_type(bus_index, dev_index,
294 		reg_index, reg_type);
295 	return result ? result
296 		: ps3_repository_read_dev_reg_addr(bus_index, dev_index,
297 		reg_index, bus_addr, len);
298 }
299 
300 
301 
302 int ps3_repository_find_device(struct ps3_repository_device *repo)
303 {
304 	int result;
305 	struct ps3_repository_device tmp = *repo;
306 	unsigned int num_dev;
307 
308 	BUG_ON(repo->bus_index > 10);
309 	BUG_ON(repo->dev_index > 10);
310 
311 	result = ps3_repository_read_bus_num_dev(tmp.bus_index, &num_dev);
312 
313 	if (result) {
314 		pr_devel("%s:%d read_bus_num_dev failed\n", __func__, __LINE__);
315 		return result;
316 	}
317 
318 	pr_devel("%s:%d: bus_type %u, bus_index %u, bus_id %llu, num_dev %u\n",
319 		__func__, __LINE__, tmp.bus_type, tmp.bus_index, tmp.bus_id,
320 		num_dev);
321 
322 	if (tmp.dev_index >= num_dev) {
323 		pr_devel("%s:%d: no device found\n", __func__, __LINE__);
324 		return -ENODEV;
325 	}
326 
327 	result = ps3_repository_read_dev_type(tmp.bus_index, tmp.dev_index,
328 		&tmp.dev_type);
329 
330 	if (result) {
331 		pr_devel("%s:%d read_dev_type failed\n", __func__, __LINE__);
332 		return result;
333 	}
334 
335 	result = ps3_repository_read_dev_id(tmp.bus_index, tmp.dev_index,
336 		&tmp.dev_id);
337 
338 	if (result) {
339 		pr_devel("%s:%d ps3_repository_read_dev_id failed\n", __func__,
340 		__LINE__);
341 		return result;
342 	}
343 
344 	pr_devel("%s:%d: found: dev_type %u, dev_index %u, dev_id %llu\n",
345 		__func__, __LINE__, tmp.dev_type, tmp.dev_index, tmp.dev_id);
346 
347 	*repo = tmp;
348 	return 0;
349 }
350 
351 int ps3_repository_find_device_by_id(struct ps3_repository_device *repo,
352 				     u64 bus_id, u64 dev_id)
353 {
354 	int result = -ENODEV;
355 	struct ps3_repository_device tmp;
356 	unsigned int num_dev;
357 
358 	pr_devel(" -> %s:%u: find device by id %llu:%llu\n", __func__, __LINE__,
359 		 bus_id, dev_id);
360 
361 	for (tmp.bus_index = 0; tmp.bus_index < 10; tmp.bus_index++) {
362 		result = ps3_repository_read_bus_id(tmp.bus_index,
363 						    &tmp.bus_id);
364 		if (result) {
365 			pr_devel("%s:%u read_bus_id(%u) failed\n", __func__,
366 				 __LINE__, tmp.bus_index);
367 			return result;
368 		}
369 
370 		if (tmp.bus_id == bus_id)
371 			goto found_bus;
372 
373 		pr_devel("%s:%u: skip, bus_id %llu\n", __func__, __LINE__,
374 			 tmp.bus_id);
375 	}
376 	pr_devel(" <- %s:%u: bus not found\n", __func__, __LINE__);
377 	return result;
378 
379 found_bus:
380 	result = ps3_repository_read_bus_type(tmp.bus_index, &tmp.bus_type);
381 	if (result) {
382 		pr_devel("%s:%u read_bus_type(%u) failed\n", __func__,
383 			 __LINE__, tmp.bus_index);
384 		return result;
385 	}
386 
387 	result = ps3_repository_read_bus_num_dev(tmp.bus_index, &num_dev);
388 	if (result) {
389 		pr_devel("%s:%u read_bus_num_dev failed\n", __func__,
390 			 __LINE__);
391 		return result;
392 	}
393 
394 	for (tmp.dev_index = 0; tmp.dev_index < num_dev; tmp.dev_index++) {
395 		result = ps3_repository_read_dev_id(tmp.bus_index,
396 						    tmp.dev_index,
397 						    &tmp.dev_id);
398 		if (result) {
399 			pr_devel("%s:%u read_dev_id(%u:%u) failed\n", __func__,
400 				 __LINE__, tmp.bus_index, tmp.dev_index);
401 			return result;
402 		}
403 
404 		if (tmp.dev_id == dev_id)
405 			goto found_dev;
406 
407 		pr_devel("%s:%u: skip, dev_id %llu\n", __func__, __LINE__,
408 			 tmp.dev_id);
409 	}
410 	pr_devel(" <- %s:%u: dev not found\n", __func__, __LINE__);
411 	return result;
412 
413 found_dev:
414 	result = ps3_repository_read_dev_type(tmp.bus_index, tmp.dev_index,
415 					      &tmp.dev_type);
416 	if (result) {
417 		pr_devel("%s:%u read_dev_type failed\n", __func__, __LINE__);
418 		return result;
419 	}
420 
421 	pr_devel(" <- %s:%u: found: type (%u:%u) index (%u:%u) id (%llu:%llu)\n",
422 		 __func__, __LINE__, tmp.bus_type, tmp.dev_type, tmp.bus_index,
423 		 tmp.dev_index, tmp.bus_id, tmp.dev_id);
424 	*repo = tmp;
425 	return 0;
426 }
427 
428 int ps3_repository_find_devices(enum ps3_bus_type bus_type,
429 	int (*callback)(const struct ps3_repository_device *repo))
430 {
431 	int result = 0;
432 	struct ps3_repository_device repo;
433 
434 	pr_devel(" -> %s:%d: find bus_type %u\n", __func__, __LINE__, bus_type);
435 
436 	repo.bus_type = bus_type;
437 	result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
438 	if (result) {
439 		pr_devel(" <- %s:%u: bus not found\n", __func__, __LINE__);
440 		return result;
441 	}
442 
443 	result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
444 	if (result) {
445 		pr_devel("%s:%d read_bus_id(%u) failed\n", __func__, __LINE__,
446 			 repo.bus_index);
447 		return result;
448 	}
449 
450 	for (repo.dev_index = 0; ; repo.dev_index++) {
451 		result = ps3_repository_find_device(&repo);
452 		if (result == -ENODEV) {
453 			result = 0;
454 			break;
455 		} else if (result)
456 			break;
457 
458 		result = callback(&repo);
459 		if (result) {
460 			pr_devel("%s:%d: abort at callback\n", __func__,
461 				__LINE__);
462 			break;
463 		}
464 	}
465 
466 	pr_devel(" <- %s:%d\n", __func__, __LINE__);
467 	return result;
468 }
469 
470 int ps3_repository_find_bus(enum ps3_bus_type bus_type, unsigned int from,
471 	unsigned int *bus_index)
472 {
473 	unsigned int i;
474 	enum ps3_bus_type type;
475 	int error;
476 
477 	for (i = from; i < 10; i++) {
478 		error = ps3_repository_read_bus_type(i, &type);
479 		if (error) {
480 			pr_devel("%s:%d read_bus_type failed\n",
481 				__func__, __LINE__);
482 			*bus_index = UINT_MAX;
483 			return error;
484 		}
485 		if (type == bus_type) {
486 			*bus_index = i;
487 			return 0;
488 		}
489 	}
490 	*bus_index = UINT_MAX;
491 	return -ENODEV;
492 }
493 
494 int ps3_repository_find_interrupt(const struct ps3_repository_device *repo,
495 	enum ps3_interrupt_type intr_type, unsigned int *interrupt_id)
496 {
497 	int result = 0;
498 	unsigned int res_index;
499 
500 	pr_devel("%s:%d: find intr_type %u\n", __func__, __LINE__, intr_type);
501 
502 	*interrupt_id = UINT_MAX;
503 
504 	for (res_index = 0; res_index < 10; res_index++) {
505 		enum ps3_interrupt_type t;
506 		unsigned int id;
507 
508 		result = ps3_repository_read_dev_intr(repo->bus_index,
509 			repo->dev_index, res_index, &t, &id);
510 
511 		if (result) {
512 			pr_devel("%s:%d read_dev_intr failed\n",
513 				__func__, __LINE__);
514 			return result;
515 		}
516 
517 		if (t == intr_type) {
518 			*interrupt_id = id;
519 			break;
520 		}
521 	}
522 
523 	if (res_index == 10)
524 		return -ENODEV;
525 
526 	pr_devel("%s:%d: found intr_type %u at res_index %u\n",
527 		__func__, __LINE__, intr_type, res_index);
528 
529 	return result;
530 }
531 
532 int ps3_repository_find_reg(const struct ps3_repository_device *repo,
533 	enum ps3_reg_type reg_type, u64 *bus_addr, u64 *len)
534 {
535 	int result = 0;
536 	unsigned int res_index;
537 
538 	pr_devel("%s:%d: find reg_type %u\n", __func__, __LINE__, reg_type);
539 
540 	*bus_addr = *len = 0;
541 
542 	for (res_index = 0; res_index < 10; res_index++) {
543 		enum ps3_reg_type t;
544 		u64 a;
545 		u64 l;
546 
547 		result = ps3_repository_read_dev_reg(repo->bus_index,
548 			repo->dev_index, res_index, &t, &a, &l);
549 
550 		if (result) {
551 			pr_devel("%s:%d read_dev_reg failed\n",
552 				__func__, __LINE__);
553 			return result;
554 		}
555 
556 		if (t == reg_type) {
557 			*bus_addr = a;
558 			*len = l;
559 			break;
560 		}
561 	}
562 
563 	if (res_index == 10)
564 		return -ENODEV;
565 
566 	pr_devel("%s:%d: found reg_type %u at res_index %u\n",
567 		__func__, __LINE__, reg_type, res_index);
568 
569 	return result;
570 }
571 
572 int ps3_repository_read_stor_dev_port(unsigned int bus_index,
573 	unsigned int dev_index, u64 *port)
574 {
575 	return read_node(PS3_LPAR_ID_PME,
576 		make_first_field("bus", bus_index),
577 		make_field("dev", dev_index),
578 		make_field("port", 0),
579 		0, port, NULL);
580 }
581 
582 int ps3_repository_read_stor_dev_blk_size(unsigned int bus_index,
583 	unsigned int dev_index, u64 *blk_size)
584 {
585 	return read_node(PS3_LPAR_ID_PME,
586 		make_first_field("bus", bus_index),
587 		make_field("dev", dev_index),
588 		make_field("blk_size", 0),
589 		0, blk_size, NULL);
590 }
591 
592 int ps3_repository_read_stor_dev_num_blocks(unsigned int bus_index,
593 	unsigned int dev_index, u64 *num_blocks)
594 {
595 	return read_node(PS3_LPAR_ID_PME,
596 		make_first_field("bus", bus_index),
597 		make_field("dev", dev_index),
598 		make_field("n_blocks", 0),
599 		0, num_blocks, NULL);
600 }
601 
602 int ps3_repository_read_stor_dev_num_regions(unsigned int bus_index,
603 	unsigned int dev_index, unsigned int *num_regions)
604 {
605 	int result;
606 	u64 v1 = 0;
607 
608 	result = read_node(PS3_LPAR_ID_PME,
609 		make_first_field("bus", bus_index),
610 		make_field("dev", dev_index),
611 		make_field("n_regs", 0),
612 		0, &v1, NULL);
613 	*num_regions = v1;
614 	return result;
615 }
616 
617 int ps3_repository_read_stor_dev_region_id(unsigned int bus_index,
618 	unsigned int dev_index, unsigned int region_index,
619 	unsigned int *region_id)
620 {
621 	int result;
622 	u64 v1 = 0;
623 
624 	result = read_node(PS3_LPAR_ID_PME,
625 	    make_first_field("bus", bus_index),
626 	    make_field("dev", dev_index),
627 	    make_field("region", region_index),
628 	    make_field("id", 0),
629 	    &v1, NULL);
630 	*region_id = v1;
631 	return result;
632 }
633 
634 int ps3_repository_read_stor_dev_region_size(unsigned int bus_index,
635 	unsigned int dev_index,	unsigned int region_index, u64 *region_size)
636 {
637 	return read_node(PS3_LPAR_ID_PME,
638 	    make_first_field("bus", bus_index),
639 	    make_field("dev", dev_index),
640 	    make_field("region", region_index),
641 	    make_field("size", 0),
642 	    region_size, NULL);
643 }
644 
645 int ps3_repository_read_stor_dev_region_start(unsigned int bus_index,
646 	unsigned int dev_index, unsigned int region_index, u64 *region_start)
647 {
648 	return read_node(PS3_LPAR_ID_PME,
649 	    make_first_field("bus", bus_index),
650 	    make_field("dev", dev_index),
651 	    make_field("region", region_index),
652 	    make_field("start", 0),
653 	    region_start, NULL);
654 }
655 
656 int ps3_repository_read_stor_dev_info(unsigned int bus_index,
657 	unsigned int dev_index, u64 *port, u64 *blk_size,
658 	u64 *num_blocks, unsigned int *num_regions)
659 {
660 	int result;
661 
662 	result = ps3_repository_read_stor_dev_port(bus_index, dev_index, port);
663 	if (result)
664 	    return result;
665 
666 	result = ps3_repository_read_stor_dev_blk_size(bus_index, dev_index,
667 		blk_size);
668 	if (result)
669 	    return result;
670 
671 	result = ps3_repository_read_stor_dev_num_blocks(bus_index, dev_index,
672 		num_blocks);
673 	if (result)
674 	    return result;
675 
676 	result = ps3_repository_read_stor_dev_num_regions(bus_index, dev_index,
677 		num_regions);
678 	return result;
679 }
680 
681 int ps3_repository_read_stor_dev_region(unsigned int bus_index,
682 	unsigned int dev_index, unsigned int region_index,
683 	unsigned int *region_id, u64 *region_start, u64 *region_size)
684 {
685 	int result;
686 
687 	result = ps3_repository_read_stor_dev_region_id(bus_index, dev_index,
688 		region_index, region_id);
689 	if (result)
690 	    return result;
691 
692 	result = ps3_repository_read_stor_dev_region_start(bus_index, dev_index,
693 		region_index, region_start);
694 	if (result)
695 	    return result;
696 
697 	result = ps3_repository_read_stor_dev_region_size(bus_index, dev_index,
698 		region_index, region_size);
699 	return result;
700 }
701 
702 /**
703  * ps3_repository_read_num_pu - Number of logical PU processors for this lpar.
704  */
705 
706 int ps3_repository_read_num_pu(u64 *num_pu)
707 {
708 	*num_pu = 0;
709 	return read_node(PS3_LPAR_ID_CURRENT,
710 			   make_first_field("bi", 0),
711 			   make_field("pun", 0),
712 			   0, 0,
713 			   num_pu, NULL);
714 }
715 
716 /**
717  * ps3_repository_read_pu_id - Read the logical PU id.
718  * @pu_index: Zero based index.
719  * @pu_id: The logical PU id.
720  */
721 
722 int ps3_repository_read_pu_id(unsigned int pu_index, u64 *pu_id)
723 {
724 	return read_node(PS3_LPAR_ID_CURRENT,
725 		make_first_field("bi", 0),
726 		make_field("pu", pu_index),
727 		0, 0,
728 		pu_id, NULL);
729 }
730 
731 int ps3_repository_read_rm_size(unsigned int ppe_id, u64 *rm_size)
732 {
733 	return read_node(PS3_LPAR_ID_CURRENT,
734 		make_first_field("bi", 0),
735 		make_field("pu", 0),
736 		ppe_id,
737 		make_field("rm_size", 0),
738 		rm_size, NULL);
739 }
740 
741 int ps3_repository_read_region_total(u64 *region_total)
742 {
743 	return read_node(PS3_LPAR_ID_CURRENT,
744 		make_first_field("bi", 0),
745 		make_field("rgntotal", 0),
746 		0, 0,
747 		region_total, NULL);
748 }
749 
750 /**
751  * ps3_repository_read_mm_info - Read mm info for single pu system.
752  * @rm_base: Real mode memory base address.
753  * @rm_size: Real mode memory size.
754  * @region_total: Maximum memory region size.
755  */
756 
757 int ps3_repository_read_mm_info(u64 *rm_base, u64 *rm_size, u64 *region_total)
758 {
759 	int result;
760 	u64 ppe_id;
761 
762 	lv1_get_logical_ppe_id(&ppe_id);
763 	*rm_base = 0;
764 	result = ps3_repository_read_rm_size(ppe_id, rm_size);
765 	return result ? result
766 		: ps3_repository_read_region_total(region_total);
767 }
768 
769 /**
770  * ps3_repository_read_highmem_region_count - Read the number of highmem regions
771  *
772  * Bootloaders must arrange the repository nodes such that regions are indexed
773  * with a region_index from 0 to region_count-1.
774  */
775 
776 int ps3_repository_read_highmem_region_count(unsigned int *region_count)
777 {
778 	int result;
779 	u64 v1 = 0;
780 
781 	result = read_node(PS3_LPAR_ID_CURRENT,
782 		make_first_field("highmem", 0),
783 		make_field("region", 0),
784 		make_field("count", 0),
785 		0,
786 		&v1, NULL);
787 	*region_count = v1;
788 	return result;
789 }
790 
791 
792 int ps3_repository_read_highmem_base(unsigned int region_index,
793 	u64 *highmem_base)
794 {
795 	return read_node(PS3_LPAR_ID_CURRENT,
796 		make_first_field("highmem", 0),
797 		make_field("region", region_index),
798 		make_field("base", 0),
799 		0,
800 		highmem_base, NULL);
801 }
802 
803 int ps3_repository_read_highmem_size(unsigned int region_index,
804 	u64 *highmem_size)
805 {
806 	return read_node(PS3_LPAR_ID_CURRENT,
807 		make_first_field("highmem", 0),
808 		make_field("region", region_index),
809 		make_field("size", 0),
810 		0,
811 		highmem_size, NULL);
812 }
813 
814 /**
815  * ps3_repository_read_highmem_info - Read high memory region info
816  * @region_index: Region index, {0,..,region_count-1}.
817  * @highmem_base: High memory base address.
818  * @highmem_size: High memory size.
819  *
820  * Bootloaders that preallocate highmem regions must place the
821  * region info into the repository at these well known nodes.
822  */
823 
824 int ps3_repository_read_highmem_info(unsigned int region_index,
825 	u64 *highmem_base, u64 *highmem_size)
826 {
827 	int result;
828 
829 	*highmem_base = 0;
830 	result = ps3_repository_read_highmem_base(region_index, highmem_base);
831 	return result ? result
832 		: ps3_repository_read_highmem_size(region_index, highmem_size);
833 }
834 
835 /**
836  * ps3_repository_read_num_spu_reserved - Number of physical spus reserved.
837  * @num_spu: Number of physical spus.
838  */
839 
840 int ps3_repository_read_num_spu_reserved(unsigned int *num_spu_reserved)
841 {
842 	int result;
843 	u64 v1 = 0;
844 
845 	result = read_node(PS3_LPAR_ID_CURRENT,
846 		make_first_field("bi", 0),
847 		make_field("spun", 0),
848 		0, 0,
849 		&v1, NULL);
850 	*num_spu_reserved = v1;
851 	return result;
852 }
853 
854 /**
855  * ps3_repository_read_num_spu_resource_id - Number of spu resource reservations.
856  * @num_resource_id: Number of spu resource ids.
857  */
858 
859 int ps3_repository_read_num_spu_resource_id(unsigned int *num_resource_id)
860 {
861 	int result;
862 	u64 v1 = 0;
863 
864 	result = read_node(PS3_LPAR_ID_CURRENT,
865 		make_first_field("bi", 0),
866 		make_field("spursvn", 0),
867 		0, 0,
868 		&v1, NULL);
869 	*num_resource_id = v1;
870 	return result;
871 }
872 
873 /**
874  * ps3_repository_read_spu_resource_id - spu resource reservation id value.
875  * @res_index: Resource reservation index.
876  * @resource_type: Resource reservation type.
877  * @resource_id: Resource reservation id.
878  */
879 
880 int ps3_repository_read_spu_resource_id(unsigned int res_index,
881 	enum ps3_spu_resource_type *resource_type, unsigned int *resource_id)
882 {
883 	int result;
884 	u64 v1 = 0;
885 	u64 v2 = 0;
886 
887 	result = read_node(PS3_LPAR_ID_CURRENT,
888 		make_first_field("bi", 0),
889 		make_field("spursv", 0),
890 		res_index,
891 		0,
892 		&v1, &v2);
893 	*resource_type = v1;
894 	*resource_id = v2;
895 	return result;
896 }
897 
898 static int ps3_repository_read_boot_dat_address(u64 *address)
899 {
900 	return read_node(PS3_LPAR_ID_CURRENT,
901 		make_first_field("bi", 0),
902 		make_field("boot_dat", 0),
903 		make_field("address", 0),
904 		0,
905 		address, NULL);
906 }
907 
908 int ps3_repository_read_boot_dat_size(unsigned int *size)
909 {
910 	int result;
911 	u64 v1 = 0;
912 
913 	result = read_node(PS3_LPAR_ID_CURRENT,
914 		make_first_field("bi", 0),
915 		make_field("boot_dat", 0),
916 		make_field("size", 0),
917 		0,
918 		&v1, NULL);
919 	*size = v1;
920 	return result;
921 }
922 
923 int ps3_repository_read_vuart_av_port(unsigned int *port)
924 {
925 	int result;
926 	u64 v1 = 0;
927 
928 	result = read_node(PS3_LPAR_ID_CURRENT,
929 		make_first_field("bi", 0),
930 		make_field("vir_uart", 0),
931 		make_field("port", 0),
932 		make_field("avset", 0),
933 		&v1, NULL);
934 	*port = v1;
935 	return result;
936 }
937 
938 int ps3_repository_read_vuart_sysmgr_port(unsigned int *port)
939 {
940 	int result;
941 	u64 v1 = 0;
942 
943 	result = read_node(PS3_LPAR_ID_CURRENT,
944 		make_first_field("bi", 0),
945 		make_field("vir_uart", 0),
946 		make_field("port", 0),
947 		make_field("sysmgr", 0),
948 		&v1, NULL);
949 	*port = v1;
950 	return result;
951 }
952 
953 /**
954   * ps3_repository_read_boot_dat_info - Get address and size of cell_ext_os_area.
955   * address: lpar address of cell_ext_os_area
956   * @size: size of cell_ext_os_area
957   */
958 
959 int ps3_repository_read_boot_dat_info(u64 *lpar_addr, unsigned int *size)
960 {
961 	int result;
962 
963 	*size = 0;
964 	result = ps3_repository_read_boot_dat_address(lpar_addr);
965 	return result ? result
966 		: ps3_repository_read_boot_dat_size(size);
967 }
968 
969 /**
970  * ps3_repository_read_num_be - Number of physical BE processors in the system.
971  */
972 
973 int ps3_repository_read_num_be(unsigned int *num_be)
974 {
975 	int result;
976 	u64 v1 = 0;
977 
978 	result = read_node(PS3_LPAR_ID_PME,
979 		make_first_field("ben", 0),
980 		0,
981 		0,
982 		0,
983 		&v1, NULL);
984 	*num_be = v1;
985 	return result;
986 }
987 
988 /**
989  * ps3_repository_read_be_node_id - Read the physical BE processor node id.
990  * @be_index: Zero based index.
991  * @node_id: The BE processor node id.
992  */
993 
994 int ps3_repository_read_be_node_id(unsigned int be_index, u64 *node_id)
995 {
996 	return read_node(PS3_LPAR_ID_PME,
997 		make_first_field("be", be_index),
998 		0,
999 		0,
1000 		0,
1001 		node_id, NULL);
1002 }
1003 
1004 /**
1005  * ps3_repository_read_be_id - Read the physical BE processor id.
1006  * @node_id: The BE processor node id.
1007  * @be_id: The BE processor id.
1008  */
1009 
1010 int ps3_repository_read_be_id(u64 node_id, u64 *be_id)
1011 {
1012 	return read_node(PS3_LPAR_ID_PME,
1013 		make_first_field("be", 0),
1014 		node_id,
1015 		0,
1016 		0,
1017 		be_id, NULL);
1018 }
1019 
1020 int ps3_repository_read_tb_freq(u64 node_id, u64 *tb_freq)
1021 {
1022 	return read_node(PS3_LPAR_ID_PME,
1023 		make_first_field("be", 0),
1024 		node_id,
1025 		make_field("clock", 0),
1026 		0,
1027 		tb_freq, NULL);
1028 }
1029 
1030 int ps3_repository_read_be_tb_freq(unsigned int be_index, u64 *tb_freq)
1031 {
1032 	int result;
1033 	u64 node_id;
1034 
1035 	*tb_freq = 0;
1036 	result = ps3_repository_read_be_node_id(be_index, &node_id);
1037 	return result ? result
1038 		: ps3_repository_read_tb_freq(node_id, tb_freq);
1039 }
1040 
1041 int ps3_repository_read_lpm_privileges(unsigned int be_index, u64 *lpar,
1042 	u64 *rights)
1043 {
1044 	int result;
1045 	u64 node_id;
1046 
1047 	*lpar = 0;
1048 	*rights = 0;
1049 	result = ps3_repository_read_be_node_id(be_index, &node_id);
1050 	return result ? result
1051 		: read_node(PS3_LPAR_ID_PME,
1052 			    make_first_field("be", 0),
1053 			    node_id,
1054 			    make_field("lpm", 0),
1055 			    make_field("priv", 0),
1056 			    lpar, rights);
1057 }
1058 
1059 #if defined(CONFIG_PS3_REPOSITORY_WRITE)
1060 
1061 static int create_node(u64 n1, u64 n2, u64 n3, u64 n4, u64 v1, u64 v2)
1062 {
1063 	int result;
1064 
1065 	dump_node(0, n1, n2, n3, n4, v1, v2);
1066 
1067 	result = lv1_create_repository_node(n1, n2, n3, n4, v1, v2);
1068 
1069 	if (result) {
1070 		pr_devel("%s:%d: lv1_create_repository_node failed: %s\n",
1071 			__func__, __LINE__, ps3_result(result));
1072 		return -ENOENT;
1073 	}
1074 
1075 	return 0;
1076 }
1077 
1078 static int delete_node(u64 n1, u64 n2, u64 n3, u64 n4)
1079 {
1080 	int result;
1081 
1082 	dump_node(0, n1, n2, n3, n4, 0, 0);
1083 
1084 	result = lv1_delete_repository_node(n1, n2, n3, n4);
1085 
1086 	if (result) {
1087 		pr_devel("%s:%d: lv1_delete_repository_node failed: %s\n",
1088 			__func__, __LINE__, ps3_result(result));
1089 		return -ENOENT;
1090 	}
1091 
1092 	return 0;
1093 }
1094 
1095 static int write_node(u64 n1, u64 n2, u64 n3, u64 n4, u64 v1, u64 v2)
1096 {
1097 	int result;
1098 
1099 	result = create_node(n1, n2, n3, n4, v1, v2);
1100 
1101 	if (!result)
1102 		return 0;
1103 
1104 	result = lv1_write_repository_node(n1, n2, n3, n4, v1, v2);
1105 
1106 	if (result) {
1107 		pr_devel("%s:%d: lv1_write_repository_node failed: %s\n",
1108 			__func__, __LINE__, ps3_result(result));
1109 		return -ENOENT;
1110 	}
1111 
1112 	return 0;
1113 }
1114 
1115 int ps3_repository_write_highmem_region_count(unsigned int region_count)
1116 {
1117 	int result;
1118 	u64 v1 = (u64)region_count;
1119 
1120 	result = write_node(
1121 		make_first_field("highmem", 0),
1122 		make_field("region", 0),
1123 		make_field("count", 0),
1124 		0,
1125 		v1, 0);
1126 	return result;
1127 }
1128 
1129 int ps3_repository_write_highmem_base(unsigned int region_index,
1130 	u64 highmem_base)
1131 {
1132 	return write_node(
1133 		make_first_field("highmem", 0),
1134 		make_field("region", region_index),
1135 		make_field("base", 0),
1136 		0,
1137 		highmem_base, 0);
1138 }
1139 
1140 int ps3_repository_write_highmem_size(unsigned int region_index,
1141 	u64 highmem_size)
1142 {
1143 	return write_node(
1144 		make_first_field("highmem", 0),
1145 		make_field("region", region_index),
1146 		make_field("size", 0),
1147 		0,
1148 		highmem_size, 0);
1149 }
1150 
1151 int ps3_repository_write_highmem_info(unsigned int region_index,
1152 	u64 highmem_base, u64 highmem_size)
1153 {
1154 	int result;
1155 
1156 	result = ps3_repository_write_highmem_base(region_index, highmem_base);
1157 	return result ? result
1158 		: ps3_repository_write_highmem_size(region_index, highmem_size);
1159 }
1160 
1161 static int ps3_repository_delete_highmem_base(unsigned int region_index)
1162 {
1163 	return delete_node(
1164 		make_first_field("highmem", 0),
1165 		make_field("region", region_index),
1166 		make_field("base", 0),
1167 		0);
1168 }
1169 
1170 static int ps3_repository_delete_highmem_size(unsigned int region_index)
1171 {
1172 	return delete_node(
1173 		make_first_field("highmem", 0),
1174 		make_field("region", region_index),
1175 		make_field("size", 0),
1176 		0);
1177 }
1178 
1179 int ps3_repository_delete_highmem_info(unsigned int region_index)
1180 {
1181 	int result;
1182 
1183 	result = ps3_repository_delete_highmem_base(region_index);
1184 	result += ps3_repository_delete_highmem_size(region_index);
1185 
1186 	return result ? -1 : 0;
1187 }
1188 
1189 #endif /* defined(CONFIG_PS3_REPOSITORY_WRITE) */
1190 
1191 #if defined(DEBUG)
1192 
1193 int ps3_repository_dump_resource_info(const struct ps3_repository_device *repo)
1194 {
1195 	int result = 0;
1196 	unsigned int res_index;
1197 
1198 	pr_devel(" -> %s:%d: (%u:%u)\n", __func__, __LINE__,
1199 		repo->bus_index, repo->dev_index);
1200 
1201 	for (res_index = 0; res_index < 10; res_index++) {
1202 		enum ps3_interrupt_type intr_type;
1203 		unsigned int interrupt_id;
1204 
1205 		result = ps3_repository_read_dev_intr(repo->bus_index,
1206 			repo->dev_index, res_index, &intr_type, &interrupt_id);
1207 
1208 		if (result) {
1209 			if (result !=  LV1_NO_ENTRY)
1210 				pr_devel("%s:%d ps3_repository_read_dev_intr"
1211 					" (%u:%u) failed\n", __func__, __LINE__,
1212 					repo->bus_index, repo->dev_index);
1213 			break;
1214 		}
1215 
1216 		pr_devel("%s:%d (%u:%u) intr_type %u, interrupt_id %u\n",
1217 			__func__, __LINE__, repo->bus_index, repo->dev_index,
1218 			intr_type, interrupt_id);
1219 	}
1220 
1221 	for (res_index = 0; res_index < 10; res_index++) {
1222 		enum ps3_reg_type reg_type;
1223 		u64 bus_addr;
1224 		u64 len;
1225 
1226 		result = ps3_repository_read_dev_reg(repo->bus_index,
1227 			repo->dev_index, res_index, &reg_type, &bus_addr, &len);
1228 
1229 		if (result) {
1230 			if (result !=  LV1_NO_ENTRY)
1231 				pr_devel("%s:%d ps3_repository_read_dev_reg"
1232 					" (%u:%u) failed\n", __func__, __LINE__,
1233 					repo->bus_index, repo->dev_index);
1234 			break;
1235 		}
1236 
1237 		pr_devel("%s:%d (%u:%u) reg_type %u, bus_addr %llxh, len %llxh\n",
1238 			__func__, __LINE__, repo->bus_index, repo->dev_index,
1239 			reg_type, bus_addr, len);
1240 	}
1241 
1242 	pr_devel(" <- %s:%d\n", __func__, __LINE__);
1243 	return result;
1244 }
1245 
1246 static int dump_stor_dev_info(struct ps3_repository_device *repo)
1247 {
1248 	int result = 0;
1249 	unsigned int num_regions, region_index;
1250 	u64 port, blk_size, num_blocks;
1251 
1252 	pr_devel(" -> %s:%d: (%u:%u)\n", __func__, __LINE__,
1253 		repo->bus_index, repo->dev_index);
1254 
1255 	result = ps3_repository_read_stor_dev_info(repo->bus_index,
1256 		repo->dev_index, &port, &blk_size, &num_blocks, &num_regions);
1257 	if (result) {
1258 		pr_devel("%s:%d ps3_repository_read_stor_dev_info"
1259 			" (%u:%u) failed\n", __func__, __LINE__,
1260 			repo->bus_index, repo->dev_index);
1261 		goto out;
1262 	}
1263 
1264 	pr_devel("%s:%d  (%u:%u): port %llu, blk_size %llu, num_blocks "
1265 		 "%llu, num_regions %u\n",
1266 		 __func__, __LINE__, repo->bus_index, repo->dev_index,
1267 		port, blk_size, num_blocks, num_regions);
1268 
1269 	for (region_index = 0; region_index < num_regions; region_index++) {
1270 		unsigned int region_id;
1271 		u64 region_start, region_size;
1272 
1273 		result = ps3_repository_read_stor_dev_region(repo->bus_index,
1274 			repo->dev_index, region_index, &region_id,
1275 			&region_start, &region_size);
1276 		if (result) {
1277 			 pr_devel("%s:%d ps3_repository_read_stor_dev_region"
1278 				  " (%u:%u) failed\n", __func__, __LINE__,
1279 				  repo->bus_index, repo->dev_index);
1280 			break;
1281 		}
1282 
1283 		pr_devel("%s:%d (%u:%u) region_id %u, start %lxh, size %lxh\n",
1284 			__func__, __LINE__, repo->bus_index, repo->dev_index,
1285 			region_id, (unsigned long)region_start,
1286 			(unsigned long)region_size);
1287 	}
1288 
1289 out:
1290 	pr_devel(" <- %s:%d\n", __func__, __LINE__);
1291 	return result;
1292 }
1293 
1294 static int dump_device_info(struct ps3_repository_device *repo,
1295 	unsigned int num_dev)
1296 {
1297 	int result = 0;
1298 
1299 	pr_devel(" -> %s:%d: bus_%u\n", __func__, __LINE__, repo->bus_index);
1300 
1301 	for (repo->dev_index = 0; repo->dev_index < num_dev;
1302 		repo->dev_index++) {
1303 
1304 		result = ps3_repository_read_dev_type(repo->bus_index,
1305 			repo->dev_index, &repo->dev_type);
1306 
1307 		if (result) {
1308 			pr_devel("%s:%d ps3_repository_read_dev_type"
1309 				" (%u:%u) failed\n", __func__, __LINE__,
1310 				repo->bus_index, repo->dev_index);
1311 			break;
1312 		}
1313 
1314 		result = ps3_repository_read_dev_id(repo->bus_index,
1315 			repo->dev_index, &repo->dev_id);
1316 
1317 		if (result) {
1318 			pr_devel("%s:%d ps3_repository_read_dev_id"
1319 				" (%u:%u) failed\n", __func__, __LINE__,
1320 				repo->bus_index, repo->dev_index);
1321 			continue;
1322 		}
1323 
1324 		pr_devel("%s:%d  (%u:%u): dev_type %u, dev_id %lu\n", __func__,
1325 			__LINE__, repo->bus_index, repo->dev_index,
1326 			repo->dev_type, (unsigned long)repo->dev_id);
1327 
1328 		ps3_repository_dump_resource_info(repo);
1329 
1330 		if (repo->bus_type == PS3_BUS_TYPE_STORAGE)
1331 			dump_stor_dev_info(repo);
1332 	}
1333 
1334 	pr_devel(" <- %s:%d\n", __func__, __LINE__);
1335 	return result;
1336 }
1337 
1338 int ps3_repository_dump_bus_info(void)
1339 {
1340 	int result = 0;
1341 	struct ps3_repository_device repo;
1342 
1343 	pr_devel(" -> %s:%d\n", __func__, __LINE__);
1344 
1345 	memset(&repo, 0, sizeof(repo));
1346 
1347 	for (repo.bus_index = 0; repo.bus_index < 10; repo.bus_index++) {
1348 		unsigned int num_dev;
1349 
1350 		result = ps3_repository_read_bus_type(repo.bus_index,
1351 			&repo.bus_type);
1352 
1353 		if (result) {
1354 			pr_devel("%s:%d read_bus_type(%u) failed\n",
1355 				__func__, __LINE__, repo.bus_index);
1356 			break;
1357 		}
1358 
1359 		result = ps3_repository_read_bus_id(repo.bus_index,
1360 			&repo.bus_id);
1361 
1362 		if (result) {
1363 			pr_devel("%s:%d read_bus_id(%u) failed\n",
1364 				__func__, __LINE__, repo.bus_index);
1365 			continue;
1366 		}
1367 
1368 		if (repo.bus_index != repo.bus_id)
1369 			pr_devel("%s:%d bus_index != bus_id\n",
1370 				__func__, __LINE__);
1371 
1372 		result = ps3_repository_read_bus_num_dev(repo.bus_index,
1373 			&num_dev);
1374 
1375 		if (result) {
1376 			pr_devel("%s:%d read_bus_num_dev(%u) failed\n",
1377 				__func__, __LINE__, repo.bus_index);
1378 			continue;
1379 		}
1380 
1381 		pr_devel("%s:%d bus_%u: bus_type %u, bus_id %lu, num_dev %u\n",
1382 			__func__, __LINE__, repo.bus_index, repo.bus_type,
1383 			(unsigned long)repo.bus_id, num_dev);
1384 
1385 		dump_device_info(&repo, num_dev);
1386 	}
1387 
1388 	pr_devel(" <- %s:%d\n", __func__, __LINE__);
1389 	return result;
1390 }
1391 
1392 #endif /* defined(DEBUG) */
1393