Pull sbs into release branch
[pandora-kernel.git] / arch / powerpc / platforms / ps3 / repository.c
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_debug("%s:%d: %s%016lx : %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_debug("%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_debug("%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_debug("%s:%d: v1: %016lx\n", func, line, v1);
74         pr_debug("%s:%d: v2: %016lx\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_get_repository_node_value(lpar_id, n1, n2, n3, n4, &v1,
135                 &v2);
136
137         if (result) {
138                 pr_debug("%s:%d: lv1_get_repository_node_value 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_debug("%s:%d: warning: discarding non-zero v1: %016lx\n",
153                         __func__, __LINE__, v1);
154         if (v2 && !_v2)
155                 pr_debug("%s:%d: warning: discarding non-zero v2: %016lx\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, 0);
169 }
170
171 int ps3_repository_read_bus_id(unsigned int bus_index, unsigned int *bus_id)
172 {
173         int result;
174         u64 v1;
175         u64 v2; /* unused */
176
177         result = read_node(PS3_LPAR_ID_PME,
178                 make_first_field("bus", bus_index),
179                 make_field("id", 0),
180                 0, 0,
181                 &v1, &v2);
182         *bus_id = v1;
183         return result;
184 }
185
186 int ps3_repository_read_bus_type(unsigned int bus_index,
187         enum ps3_bus_type *bus_type)
188 {
189         int result;
190         u64 v1;
191
192         result = read_node(PS3_LPAR_ID_PME,
193                 make_first_field("bus", bus_index),
194                 make_field("type", 0),
195                 0, 0,
196                 &v1, 0);
197         *bus_type = v1;
198         return result;
199 }
200
201 int ps3_repository_read_bus_num_dev(unsigned int bus_index,
202         unsigned int *num_dev)
203 {
204         int result;
205         u64 v1;
206
207         result = read_node(PS3_LPAR_ID_PME,
208                 make_first_field("bus", bus_index),
209                 make_field("num_dev", 0),
210                 0, 0,
211                 &v1, 0);
212         *num_dev = v1;
213         return result;
214 }
215
216 int ps3_repository_read_dev_str(unsigned int bus_index,
217         unsigned int dev_index, const char *dev_str, u64 *value)
218 {
219         return read_node(PS3_LPAR_ID_PME,
220                 make_first_field("bus", bus_index),
221                 make_field("dev", dev_index),
222                 make_field(dev_str, 0),
223                 0,
224                 value, 0);
225 }
226
227 int ps3_repository_read_dev_id(unsigned int bus_index, unsigned int dev_index,
228         unsigned int *dev_id)
229 {
230         int result;
231         u64 v1;
232
233         result = read_node(PS3_LPAR_ID_PME,
234                 make_first_field("bus", bus_index),
235                 make_field("dev", dev_index),
236                 make_field("id", 0),
237                 0,
238                 &v1, 0);
239         *dev_id = v1;
240         return result;
241 }
242
243 int ps3_repository_read_dev_type(unsigned int bus_index,
244         unsigned int dev_index, enum ps3_dev_type *dev_type)
245 {
246         int result;
247         u64 v1;
248
249         result = read_node(PS3_LPAR_ID_PME,
250                 make_first_field("bus", bus_index),
251                 make_field("dev", dev_index),
252                 make_field("type", 0),
253                 0,
254                 &v1, 0);
255         *dev_type = v1;
256         return result;
257 }
258
259 int ps3_repository_read_dev_intr(unsigned int bus_index,
260         unsigned int dev_index, unsigned int intr_index,
261         enum ps3_interrupt_type *intr_type, unsigned int* interrupt_id)
262 {
263         int result;
264         u64 v1;
265         u64 v2;
266
267         result = read_node(PS3_LPAR_ID_PME,
268                 make_first_field("bus", bus_index),
269                 make_field("dev", dev_index),
270                 make_field("intr", intr_index),
271                 0,
272                 &v1, &v2);
273         *intr_type = v1;
274         *interrupt_id = v2;
275         return result;
276 }
277
278 int ps3_repository_read_dev_reg_type(unsigned int bus_index,
279         unsigned int dev_index, unsigned int reg_index,
280         enum ps3_reg_type *reg_type)
281 {
282         int result;
283         u64 v1;
284
285         result = read_node(PS3_LPAR_ID_PME,
286                 make_first_field("bus", bus_index),
287                 make_field("dev", dev_index),
288                 make_field("reg", reg_index),
289                 make_field("type", 0),
290                 &v1, 0);
291         *reg_type = v1;
292         return result;
293 }
294
295 int ps3_repository_read_dev_reg_addr(unsigned int bus_index,
296         unsigned int dev_index, unsigned int reg_index, u64 *bus_addr, u64 *len)
297 {
298         return read_node(PS3_LPAR_ID_PME,
299                 make_first_field("bus", bus_index),
300                 make_field("dev", dev_index),
301                 make_field("reg", reg_index),
302                 make_field("data", 0),
303                 bus_addr, len);
304 }
305
306 int ps3_repository_read_dev_reg(unsigned int bus_index,
307         unsigned int dev_index, unsigned int reg_index,
308         enum ps3_reg_type *reg_type, u64 *bus_addr, u64 *len)
309 {
310         int result = ps3_repository_read_dev_reg_type(bus_index, dev_index,
311                 reg_index, reg_type);
312         return result ? result
313                 : ps3_repository_read_dev_reg_addr(bus_index, dev_index,
314                 reg_index, bus_addr, len);
315 }
316
317
318
319 int ps3_repository_find_device(struct ps3_repository_device *repo)
320 {
321         int result;
322         struct ps3_repository_device tmp = *repo;
323         unsigned int num_dev;
324
325         BUG_ON(repo->bus_index > 10);
326         BUG_ON(repo->dev_index > 10);
327
328         result = ps3_repository_read_bus_num_dev(tmp.bus_index, &num_dev);
329
330         if (result) {
331                 pr_debug("%s:%d read_bus_num_dev failed\n", __func__, __LINE__);
332                 return result;
333         }
334
335         pr_debug("%s:%d: bus_type %u, bus_index %u, bus_id %u, num_dev %u\n",
336                 __func__, __LINE__, tmp.bus_type, tmp.bus_index, tmp.bus_id,
337                 num_dev);
338
339         if (tmp.dev_index >= num_dev) {
340                 pr_debug("%s:%d: no device found\n", __func__, __LINE__);
341                 return -ENODEV;
342         }
343
344         result = ps3_repository_read_dev_type(tmp.bus_index, tmp.dev_index,
345                 &tmp.dev_type);
346
347         if (result) {
348                 pr_debug("%s:%d read_dev_type failed\n", __func__, __LINE__);
349                 return result;
350         }
351
352         result = ps3_repository_read_dev_id(tmp.bus_index, tmp.dev_index,
353                 &tmp.dev_id);
354
355         if (result) {
356                 pr_debug("%s:%d ps3_repository_read_dev_id failed\n", __func__,
357                 __LINE__);
358                 return result;
359         }
360
361         pr_debug("%s:%d: found: dev_type %u, dev_index %u, dev_id %u\n",
362                 __func__, __LINE__, tmp.dev_type, tmp.dev_index, tmp.dev_id);
363
364         *repo = tmp;
365         return 0;
366 }
367
368 int __devinit ps3_repository_find_devices(enum ps3_bus_type bus_type,
369         int (*callback)(const struct ps3_repository_device *repo))
370 {
371         int result = 0;
372         struct ps3_repository_device repo;
373
374         pr_debug(" -> %s:%d: find bus_type %u\n", __func__, __LINE__, bus_type);
375
376         for (repo.bus_index = 0; repo.bus_index < 10; repo.bus_index++) {
377
378                 result = ps3_repository_read_bus_type(repo.bus_index,
379                         &repo.bus_type);
380
381                 if (result) {
382                         pr_debug("%s:%d read_bus_type(%u) failed\n",
383                                 __func__, __LINE__, repo.bus_index);
384                         break;
385                 }
386
387                 if (repo.bus_type != bus_type) {
388                         pr_debug("%s:%d: skip, bus_type %u\n", __func__,
389                                 __LINE__, repo.bus_type);
390                         continue;
391                 }
392
393                 result = ps3_repository_read_bus_id(repo.bus_index,
394                         &repo.bus_id);
395
396                 if (result) {
397                         pr_debug("%s:%d read_bus_id(%u) failed\n",
398                                 __func__, __LINE__, repo.bus_index);
399                         continue;
400                 }
401
402                 for (repo.dev_index = 0; ; repo.dev_index++) {
403                         result = ps3_repository_find_device(&repo);
404
405                         if (result == -ENODEV) {
406                                 result = 0;
407                                 break;
408                         } else if (result)
409                                 break;
410
411                         result = callback(&repo);
412
413                         if (result) {
414                                 pr_debug("%s:%d: abort at callback\n", __func__,
415                                         __LINE__);
416                                 break;
417                         }
418                 }
419                 break;
420         }
421
422         pr_debug(" <- %s:%d\n", __func__, __LINE__);
423         return result;
424 }
425
426 int ps3_repository_find_bus(enum ps3_bus_type bus_type, unsigned int from,
427         unsigned int *bus_index)
428 {
429         unsigned int i;
430         enum ps3_bus_type type;
431         int error;
432
433         for (i = from; i < 10; i++) {
434                 error = ps3_repository_read_bus_type(i, &type);
435                 if (error) {
436                         pr_debug("%s:%d read_bus_type failed\n",
437                                 __func__, __LINE__);
438                         *bus_index = UINT_MAX;
439                         return error;
440                 }
441                 if (type == bus_type) {
442                         *bus_index = i;
443                         return 0;
444                 }
445         }
446         *bus_index = UINT_MAX;
447         return -ENODEV;
448 }
449
450 int ps3_repository_find_interrupt(const struct ps3_repository_device *repo,
451         enum ps3_interrupt_type intr_type, unsigned int *interrupt_id)
452 {
453         int result = 0;
454         unsigned int res_index;
455
456         pr_debug("%s:%d: find intr_type %u\n", __func__, __LINE__, intr_type);
457
458         *interrupt_id = UINT_MAX;
459
460         for (res_index = 0; res_index < 10; res_index++) {
461                 enum ps3_interrupt_type t;
462                 unsigned int id;
463
464                 result = ps3_repository_read_dev_intr(repo->bus_index,
465                         repo->dev_index, res_index, &t, &id);
466
467                 if (result) {
468                         pr_debug("%s:%d read_dev_intr failed\n",
469                                 __func__, __LINE__);
470                         return result;
471                 }
472
473                 if (t == intr_type) {
474                         *interrupt_id = id;
475                         break;
476                 }
477         }
478
479         if (res_index == 10)
480                 return -ENODEV;
481
482         pr_debug("%s:%d: found intr_type %u at res_index %u\n",
483                 __func__, __LINE__, intr_type, res_index);
484
485         return result;
486 }
487
488 int ps3_repository_find_reg(const struct ps3_repository_device *repo,
489         enum ps3_reg_type reg_type, u64 *bus_addr, u64 *len)
490 {
491         int result = 0;
492         unsigned int res_index;
493
494         pr_debug("%s:%d: find reg_type %u\n", __func__, __LINE__, reg_type);
495
496         *bus_addr = *len = 0;
497
498         for (res_index = 0; res_index < 10; res_index++) {
499                 enum ps3_reg_type t;
500                 u64 a;
501                 u64 l;
502
503                 result = ps3_repository_read_dev_reg(repo->bus_index,
504                         repo->dev_index, res_index, &t, &a, &l);
505
506                 if (result) {
507                         pr_debug("%s:%d read_dev_reg failed\n",
508                                 __func__, __LINE__);
509                         return result;
510                 }
511
512                 if (t == reg_type) {
513                         *bus_addr = a;
514                         *len = l;
515                         break;
516                 }
517         }
518
519         if (res_index == 10)
520                 return -ENODEV;
521
522         pr_debug("%s:%d: found reg_type %u at res_index %u\n",
523                 __func__, __LINE__, reg_type, res_index);
524
525         return result;
526 }
527
528 int ps3_repository_read_stor_dev_port(unsigned int bus_index,
529         unsigned int dev_index, u64 *port)
530 {
531         return read_node(PS3_LPAR_ID_PME,
532                 make_first_field("bus", bus_index),
533                 make_field("dev", dev_index),
534                 make_field("port", 0),
535                 0, port, 0);
536 }
537
538 int ps3_repository_read_stor_dev_blk_size(unsigned int bus_index,
539         unsigned int dev_index, u64 *blk_size)
540 {
541         return read_node(PS3_LPAR_ID_PME,
542                 make_first_field("bus", bus_index),
543                 make_field("dev", dev_index),
544                 make_field("blk_size", 0),
545                 0, blk_size, 0);
546 }
547
548 int ps3_repository_read_stor_dev_num_blocks(unsigned int bus_index,
549         unsigned int dev_index, u64 *num_blocks)
550 {
551         return read_node(PS3_LPAR_ID_PME,
552                 make_first_field("bus", bus_index),
553                 make_field("dev", dev_index),
554                 make_field("n_blocks", 0),
555                 0, num_blocks, 0);
556 }
557
558 int ps3_repository_read_stor_dev_num_regions(unsigned int bus_index,
559         unsigned int dev_index, unsigned int *num_regions)
560 {
561         int result;
562         u64 v1;
563
564         result = read_node(PS3_LPAR_ID_PME,
565                 make_first_field("bus", bus_index),
566                 make_field("dev", dev_index),
567                 make_field("n_regs", 0),
568                 0, &v1, 0);
569         *num_regions = v1;
570         return result;
571 }
572
573 int ps3_repository_read_stor_dev_region_id(unsigned int bus_index,
574         unsigned int dev_index, unsigned int region_index,
575         unsigned int *region_id)
576 {
577         int result;
578         u64 v1;
579
580         result = read_node(PS3_LPAR_ID_PME,
581             make_first_field("bus", bus_index),
582             make_field("dev", dev_index),
583             make_field("region", region_index),
584             make_field("id", 0),
585             &v1, 0);
586         *region_id = v1;
587         return result;
588 }
589
590 int ps3_repository_read_stor_dev_region_size(unsigned int bus_index,
591         unsigned int dev_index, unsigned int region_index, u64 *region_size)
592 {
593         return read_node(PS3_LPAR_ID_PME,
594             make_first_field("bus", bus_index),
595             make_field("dev", dev_index),
596             make_field("region", region_index),
597             make_field("size", 0),
598             region_size, 0);
599 }
600
601 int ps3_repository_read_stor_dev_region_start(unsigned int bus_index,
602         unsigned int dev_index, unsigned int region_index, u64 *region_start)
603 {
604         return read_node(PS3_LPAR_ID_PME,
605             make_first_field("bus", bus_index),
606             make_field("dev", dev_index),
607             make_field("region", region_index),
608             make_field("start", 0),
609             region_start, 0);
610 }
611
612 int ps3_repository_read_stor_dev_info(unsigned int bus_index,
613         unsigned int dev_index, u64 *port, u64 *blk_size,
614         u64 *num_blocks, unsigned int *num_regions)
615 {
616         int result;
617
618         result = ps3_repository_read_stor_dev_port(bus_index, dev_index, port);
619         if (result)
620             return result;
621
622         result = ps3_repository_read_stor_dev_blk_size(bus_index, dev_index,
623                 blk_size);
624         if (result)
625             return result;
626
627         result = ps3_repository_read_stor_dev_num_blocks(bus_index, dev_index,
628                 num_blocks);
629         if (result)
630             return result;
631
632         result = ps3_repository_read_stor_dev_num_regions(bus_index, dev_index,
633                 num_regions);
634         return result;
635 }
636
637 int ps3_repository_read_stor_dev_region(unsigned int bus_index,
638         unsigned int dev_index, unsigned int region_index,
639         unsigned int *region_id, u64 *region_start, u64 *region_size)
640 {
641         int result;
642
643         result = ps3_repository_read_stor_dev_region_id(bus_index, dev_index,
644                 region_index, region_id);
645         if (result)
646             return result;
647
648         result = ps3_repository_read_stor_dev_region_start(bus_index, dev_index,
649                 region_index, region_start);
650         if (result)
651             return result;
652
653         result = ps3_repository_read_stor_dev_region_size(bus_index, dev_index,
654                 region_index, region_size);
655         return result;
656 }
657
658 int ps3_repository_read_rm_size(unsigned int ppe_id, u64 *rm_size)
659 {
660         return read_node(PS3_LPAR_ID_CURRENT,
661                 make_first_field("bi", 0),
662                 make_field("pu", 0),
663                 ppe_id,
664                 make_field("rm_size", 0),
665                 rm_size, 0);
666 }
667
668 int ps3_repository_read_region_total(u64 *region_total)
669 {
670         return read_node(PS3_LPAR_ID_CURRENT,
671                 make_first_field("bi", 0),
672                 make_field("rgntotal", 0),
673                 0, 0,
674                 region_total, 0);
675 }
676
677 /**
678  * ps3_repository_read_mm_info - Read mm info for single pu system.
679  * @rm_base: Real mode memory base address.
680  * @rm_size: Real mode memory size.
681  * @region_total: Maximum memory region size.
682  */
683
684 int ps3_repository_read_mm_info(u64 *rm_base, u64 *rm_size, u64 *region_total)
685 {
686         int result;
687         u64 ppe_id;
688
689         lv1_get_logical_ppe_id(&ppe_id);
690         *rm_base = 0;
691         result = ps3_repository_read_rm_size(ppe_id, rm_size);
692         return result ? result
693                 : ps3_repository_read_region_total(region_total);
694 }
695
696 /**
697  * ps3_repository_read_num_spu_reserved - Number of physical spus reserved.
698  * @num_spu: Number of physical spus.
699  */
700
701 int ps3_repository_read_num_spu_reserved(unsigned int *num_spu_reserved)
702 {
703         int result;
704         u64 v1;
705
706         result = read_node(PS3_LPAR_ID_CURRENT,
707                 make_first_field("bi", 0),
708                 make_field("spun", 0),
709                 0, 0,
710                 &v1, 0);
711         *num_spu_reserved = v1;
712         return result;
713 }
714
715 /**
716  * ps3_repository_read_num_spu_resource_id - Number of spu resource reservations.
717  * @num_resource_id: Number of spu resource ids.
718  */
719
720 int ps3_repository_read_num_spu_resource_id(unsigned int *num_resource_id)
721 {
722         int result;
723         u64 v1;
724
725         result = read_node(PS3_LPAR_ID_CURRENT,
726                 make_first_field("bi", 0),
727                 make_field("spursvn", 0),
728                 0, 0,
729                 &v1, 0);
730         *num_resource_id = v1;
731         return result;
732 }
733
734 /**
735  * ps3_repository_read_spu_resource_id - spu resource reservation id value.
736  * @res_index: Resource reservation index.
737  * @resource_type: Resource reservation type.
738  * @resource_id: Resource reservation id.
739  */
740
741 int ps3_repository_read_spu_resource_id(unsigned int res_index,
742         enum ps3_spu_resource_type* resource_type, unsigned int *resource_id)
743 {
744         int result;
745         u64 v1;
746         u64 v2;
747
748         result = read_node(PS3_LPAR_ID_CURRENT,
749                 make_first_field("bi", 0),
750                 make_field("spursv", 0),
751                 res_index,
752                 0,
753                 &v1, &v2);
754         *resource_type = v1;
755         *resource_id = v2;
756         return result;
757 }
758
759 int ps3_repository_read_boot_dat_address(u64 *address)
760 {
761         return read_node(PS3_LPAR_ID_CURRENT,
762                 make_first_field("bi", 0),
763                 make_field("boot_dat", 0),
764                 make_field("address", 0),
765                 0,
766                 address, 0);
767 }
768
769 int ps3_repository_read_boot_dat_size(unsigned int *size)
770 {
771         int result;
772         u64 v1;
773
774         result = read_node(PS3_LPAR_ID_CURRENT,
775                 make_first_field("bi", 0),
776                 make_field("boot_dat", 0),
777                 make_field("size", 0),
778                 0,
779                 &v1, 0);
780         *size = v1;
781         return result;
782 }
783
784 int ps3_repository_read_vuart_av_port(unsigned int *port)
785 {
786         int result;
787         u64 v1;
788
789         result = read_node(PS3_LPAR_ID_CURRENT,
790                 make_first_field("bi", 0),
791                 make_field("vir_uart", 0),
792                 make_field("port", 0),
793                 make_field("avset", 0),
794                 &v1, 0);
795         *port = v1;
796         return result;
797 }
798
799 int ps3_repository_read_vuart_sysmgr_port(unsigned int *port)
800 {
801         int result;
802         u64 v1;
803
804         result = read_node(PS3_LPAR_ID_CURRENT,
805                 make_first_field("bi", 0),
806                 make_field("vir_uart", 0),
807                 make_field("port", 0),
808                 make_field("sysmgr", 0),
809                 &v1, 0);
810         *port = v1;
811         return result;
812 }
813
814 /**
815   * ps3_repository_read_boot_dat_info - Get address and size of cell_ext_os_area.
816   * address: lpar address of cell_ext_os_area
817   * @size: size of cell_ext_os_area
818   */
819
820 int ps3_repository_read_boot_dat_info(u64 *lpar_addr, unsigned int *size)
821 {
822         int result;
823
824         *size = 0;
825         result = ps3_repository_read_boot_dat_address(lpar_addr);
826         return result ? result
827                 : ps3_repository_read_boot_dat_size(size);
828 }
829
830 int ps3_repository_read_num_be(unsigned int *num_be)
831 {
832         int result;
833         u64 v1;
834
835         result = read_node(PS3_LPAR_ID_PME,
836                 make_first_field("ben", 0),
837                 0,
838                 0,
839                 0,
840                 &v1, 0);
841         *num_be = v1;
842         return result;
843 }
844
845 int ps3_repository_read_be_node_id(unsigned int be_index, u64 *node_id)
846 {
847         return read_node(PS3_LPAR_ID_PME,
848                 make_first_field("be", be_index),
849                 0,
850                 0,
851                 0,
852                 node_id, 0);
853 }
854
855 int ps3_repository_read_tb_freq(u64 node_id, u64 *tb_freq)
856 {
857         return read_node(PS3_LPAR_ID_PME,
858                 make_first_field("be", 0),
859                 node_id,
860                 make_field("clock", 0),
861                 0,
862                 tb_freq, 0);
863 }
864
865 int ps3_repository_read_be_tb_freq(unsigned int be_index, u64 *tb_freq)
866 {
867         int result;
868         u64 node_id;
869
870         *tb_freq = 0;
871         result = ps3_repository_read_be_node_id(0, &node_id);
872         return result ? result
873                 : ps3_repository_read_tb_freq(node_id, tb_freq);
874 }
875
876 #if defined(DEBUG)
877
878 int ps3_repository_dump_resource_info(const struct ps3_repository_device *repo)
879 {
880         int result = 0;
881         unsigned int res_index;
882
883         pr_debug(" -> %s:%d: (%u:%u)\n", __func__, __LINE__,
884                 repo->bus_index, repo->dev_index);
885
886         for (res_index = 0; res_index < 10; res_index++) {
887                 enum ps3_interrupt_type intr_type;
888                 unsigned int interrupt_id;
889
890                 result = ps3_repository_read_dev_intr(repo->bus_index,
891                         repo->dev_index, res_index, &intr_type, &interrupt_id);
892
893                 if (result) {
894                         if (result !=  LV1_NO_ENTRY)
895                                 pr_debug("%s:%d ps3_repository_read_dev_intr"
896                                         " (%u:%u) failed\n", __func__, __LINE__,
897                                         repo->bus_index, repo->dev_index);
898                         break;
899                 }
900
901                 pr_debug("%s:%d (%u:%u) intr_type %u, interrupt_id %u\n",
902                         __func__, __LINE__, repo->bus_index, repo->dev_index,
903                         intr_type, interrupt_id);
904         }
905
906         for (res_index = 0; res_index < 10; res_index++) {
907                 enum ps3_reg_type reg_type;
908                 u64 bus_addr;
909                 u64 len;
910
911                 result = ps3_repository_read_dev_reg(repo->bus_index,
912                         repo->dev_index, res_index, &reg_type, &bus_addr, &len);
913
914                 if (result) {
915                         if (result !=  LV1_NO_ENTRY)
916                                 pr_debug("%s:%d ps3_repository_read_dev_reg"
917                                         " (%u:%u) failed\n", __func__, __LINE__,
918                                         repo->bus_index, repo->dev_index);
919                         break;
920                 }
921
922                 pr_debug("%s:%d (%u:%u) reg_type %u, bus_addr %lxh, len %lxh\n",
923                         __func__, __LINE__, repo->bus_index, repo->dev_index,
924                         reg_type, bus_addr, len);
925         }
926
927         pr_debug(" <- %s:%d\n", __func__, __LINE__);
928         return result;
929 }
930
931 static int dump_stor_dev_info(struct ps3_repository_device *repo)
932 {
933         int result = 0;
934         unsigned int num_regions, region_index;
935         u64 port, blk_size, num_blocks;
936
937         pr_debug(" -> %s:%d: (%u:%u)\n", __func__, __LINE__,
938                 repo->bus_index, repo->dev_index);
939
940         result = ps3_repository_read_stor_dev_info(repo->bus_index,
941                 repo->dev_index, &port, &blk_size, &num_blocks, &num_regions);
942         if (result) {
943                 pr_debug("%s:%d ps3_repository_read_stor_dev_info"
944                         " (%u:%u) failed\n", __func__, __LINE__,
945                         repo->bus_index, repo->dev_index);
946                 goto out;
947         }
948
949         pr_debug("%s:%d  (%u:%u): port %lu, blk_size %lu, num_blocks "
950                  "%lu, num_regions %u\n",
951                  __func__, __LINE__, repo->bus_index, repo->dev_index, port,
952                  blk_size, num_blocks, num_regions);
953
954         for (region_index = 0; region_index < num_regions; region_index++) {
955                 unsigned int region_id;
956                 u64 region_start, region_size;
957
958                 result = ps3_repository_read_stor_dev_region(repo->bus_index,
959                         repo->dev_index, region_index, &region_id,
960                         &region_start, &region_size);
961                 if (result) {
962                          pr_debug("%s:%d ps3_repository_read_stor_dev_region"
963                                   " (%u:%u) failed\n", __func__, __LINE__,
964                                   repo->bus_index, repo->dev_index);
965                         break;
966                 }
967
968                 pr_debug("%s:%d (%u:%u) region_id %u, start %lxh, size %lxh\n",
969                         __func__, __LINE__, repo->bus_index, repo->dev_index,
970                         region_id, region_start, region_size);
971         }
972
973 out:
974         pr_debug(" <- %s:%d\n", __func__, __LINE__);
975         return result;
976 }
977
978 static int dump_device_info(struct ps3_repository_device *repo,
979         unsigned int num_dev)
980 {
981         int result = 0;
982
983         pr_debug(" -> %s:%d: bus_%u\n", __func__, __LINE__, repo->bus_index);
984
985         for (repo->dev_index = 0; repo->dev_index < num_dev;
986                 repo->dev_index++) {
987
988                 result = ps3_repository_read_dev_type(repo->bus_index,
989                         repo->dev_index, &repo->dev_type);
990
991                 if (result) {
992                         pr_debug("%s:%d ps3_repository_read_dev_type"
993                                 " (%u:%u) failed\n", __func__, __LINE__,
994                                 repo->bus_index, repo->dev_index);
995                         break;
996                 }
997
998                 result = ps3_repository_read_dev_id(repo->bus_index,
999                         repo->dev_index, &repo->dev_id);
1000
1001                 if (result) {
1002                         pr_debug("%s:%d ps3_repository_read_dev_id"
1003                                 " (%u:%u) failed\n", __func__, __LINE__,
1004                                 repo->bus_index, repo->dev_index);
1005                         continue;
1006                 }
1007
1008                 pr_debug("%s:%d  (%u:%u): dev_type %u, dev_id %u\n", __func__,
1009                         __LINE__, repo->bus_index, repo->dev_index,
1010                         repo->dev_type, repo->dev_id);
1011
1012                 ps3_repository_dump_resource_info(repo);
1013
1014                 if (repo->bus_type == PS3_BUS_TYPE_STORAGE)
1015                         dump_stor_dev_info(repo);
1016         }
1017
1018         pr_debug(" <- %s:%d\n", __func__, __LINE__);
1019         return result;
1020 }
1021
1022 int ps3_repository_dump_bus_info(void)
1023 {
1024         int result = 0;
1025         struct ps3_repository_device repo;
1026
1027         pr_debug(" -> %s:%d\n", __func__, __LINE__);
1028
1029         memset(&repo, 0, sizeof(repo));
1030
1031         for (repo.bus_index = 0; repo.bus_index < 10; repo.bus_index++) {
1032                 unsigned int num_dev;
1033
1034                 result = ps3_repository_read_bus_type(repo.bus_index,
1035                         &repo.bus_type);
1036
1037                 if (result) {
1038                         pr_debug("%s:%d read_bus_type(%u) failed\n",
1039                                 __func__, __LINE__, repo.bus_index);
1040                         break;
1041                 }
1042
1043                 result = ps3_repository_read_bus_id(repo.bus_index,
1044                         &repo.bus_id);
1045
1046                 if (result) {
1047                         pr_debug("%s:%d read_bus_id(%u) failed\n",
1048                                 __func__, __LINE__, repo.bus_index);
1049                         continue;
1050                 }
1051
1052                 if (repo.bus_index != repo.bus_id)
1053                         pr_debug("%s:%d bus_index != bus_id\n",
1054                                 __func__, __LINE__);
1055
1056                 result = ps3_repository_read_bus_num_dev(repo.bus_index,
1057                         &num_dev);
1058
1059                 if (result) {
1060                         pr_debug("%s:%d read_bus_num_dev(%u) failed\n",
1061                                 __func__, __LINE__, repo.bus_index);
1062                         continue;
1063                 }
1064
1065                 pr_debug("%s:%d bus_%u: bus_type %u, bus_id %u, num_dev %u\n",
1066                         __func__, __LINE__, repo.bus_index, repo.bus_type,
1067                         repo.bus_id, num_dev);
1068
1069                 dump_device_info(&repo, num_dev);
1070         }
1071
1072         pr_debug(" <- %s:%d\n", __func__, __LINE__);
1073         return result;
1074 }
1075
1076 #endif /* defined(DEBUG) */