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