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