device-init.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /*
  2. * PS3 device registration routines.
  3. *
  4. * Copyright (C) 2007 Sony Computer Entertainment Inc.
  5. * Copyright 2007 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. #include <linux/delay.h>
  21. #include <linux/freezer.h>
  22. #include <linux/kernel.h>
  23. #include <linux/kthread.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/reboot.h>
  27. #include <asm/firmware.h>
  28. #include <asm/lv1call.h>
  29. #include <asm/ps3stor.h>
  30. #include "platform.h"
  31. static int __init ps3_register_lpm_devices(void)
  32. {
  33. int result;
  34. u64 tmp1;
  35. u64 tmp2;
  36. struct ps3_system_bus_device *dev;
  37. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  38. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  39. if (!dev)
  40. return -ENOMEM;
  41. dev->match_id = PS3_MATCH_ID_LPM;
  42. dev->dev_type = PS3_DEVICE_TYPE_LPM;
  43. /* The current lpm driver only supports a single BE processor. */
  44. result = ps3_repository_read_be_node_id(0, &dev->lpm.node_id);
  45. if (result) {
  46. pr_debug("%s:%d: ps3_repository_read_be_node_id failed \n",
  47. __func__, __LINE__);
  48. goto fail_read_repo;
  49. }
  50. result = ps3_repository_read_lpm_privileges(dev->lpm.node_id, &tmp1,
  51. &dev->lpm.rights);
  52. if (result) {
  53. pr_debug("%s:%d: ps3_repository_read_lpm_privleges failed \n",
  54. __func__, __LINE__);
  55. goto fail_read_repo;
  56. }
  57. lv1_get_logical_partition_id(&tmp2);
  58. if (tmp1 != tmp2) {
  59. pr_debug("%s:%d: wrong lpar\n",
  60. __func__, __LINE__);
  61. result = -ENODEV;
  62. goto fail_rights;
  63. }
  64. if (!(dev->lpm.rights & PS3_LPM_RIGHTS_USE_LPM)) {
  65. pr_debug("%s:%d: don't have rights to use lpm\n",
  66. __func__, __LINE__);
  67. result = -EPERM;
  68. goto fail_rights;
  69. }
  70. pr_debug("%s:%d: pu_id %llu, rights %llu(%llxh)\n",
  71. __func__, __LINE__, dev->lpm.pu_id, dev->lpm.rights,
  72. dev->lpm.rights);
  73. result = ps3_repository_read_pu_id(0, &dev->lpm.pu_id);
  74. if (result) {
  75. pr_debug("%s:%d: ps3_repository_read_pu_id failed \n",
  76. __func__, __LINE__);
  77. goto fail_read_repo;
  78. }
  79. result = ps3_system_bus_device_register(dev);
  80. if (result) {
  81. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  82. __func__, __LINE__);
  83. goto fail_register;
  84. }
  85. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  86. return 0;
  87. fail_register:
  88. fail_rights:
  89. fail_read_repo:
  90. kfree(dev);
  91. pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
  92. return result;
  93. }
  94. /**
  95. * ps3_setup_gelic_device - Setup and register a gelic device instance.
  96. *
  97. * Allocates memory for a struct ps3_system_bus_device instance, initialises the
  98. * structure members, and registers the device instance with the system bus.
  99. */
  100. static int __init ps3_setup_gelic_device(
  101. const struct ps3_repository_device *repo)
  102. {
  103. int result;
  104. struct layout {
  105. struct ps3_system_bus_device dev;
  106. struct ps3_dma_region d_region;
  107. } *p;
  108. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  109. BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
  110. BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_GELIC);
  111. p = kzalloc(sizeof(struct layout), GFP_KERNEL);
  112. if (!p) {
  113. result = -ENOMEM;
  114. goto fail_malloc;
  115. }
  116. p->dev.match_id = PS3_MATCH_ID_GELIC;
  117. p->dev.dev_type = PS3_DEVICE_TYPE_SB;
  118. p->dev.bus_id = repo->bus_id;
  119. p->dev.dev_id = repo->dev_id;
  120. p->dev.d_region = &p->d_region;
  121. result = ps3_repository_find_interrupt(repo,
  122. PS3_INTERRUPT_TYPE_EVENT_PORT, &p->dev.interrupt_id);
  123. if (result) {
  124. pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
  125. __func__, __LINE__);
  126. goto fail_find_interrupt;
  127. }
  128. BUG_ON(p->dev.interrupt_id != 0);
  129. result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
  130. PS3_DMA_OTHER, NULL, 0);
  131. if (result) {
  132. pr_debug("%s:%d ps3_dma_region_init failed\n",
  133. __func__, __LINE__);
  134. goto fail_dma_init;
  135. }
  136. result = ps3_system_bus_device_register(&p->dev);
  137. if (result) {
  138. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  139. __func__, __LINE__);
  140. goto fail_device_register;
  141. }
  142. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  143. return result;
  144. fail_device_register:
  145. fail_dma_init:
  146. fail_find_interrupt:
  147. kfree(p);
  148. fail_malloc:
  149. pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
  150. return result;
  151. }
  152. static int __init_refok ps3_setup_uhc_device(
  153. const struct ps3_repository_device *repo, enum ps3_match_id match_id,
  154. enum ps3_interrupt_type interrupt_type, enum ps3_reg_type reg_type)
  155. {
  156. int result;
  157. struct layout {
  158. struct ps3_system_bus_device dev;
  159. struct ps3_dma_region d_region;
  160. struct ps3_mmio_region m_region;
  161. } *p;
  162. u64 bus_addr;
  163. u64 len;
  164. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  165. BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
  166. BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_USB);
  167. p = kzalloc(sizeof(struct layout), GFP_KERNEL);
  168. if (!p) {
  169. result = -ENOMEM;
  170. goto fail_malloc;
  171. }
  172. p->dev.match_id = match_id;
  173. p->dev.dev_type = PS3_DEVICE_TYPE_SB;
  174. p->dev.bus_id = repo->bus_id;
  175. p->dev.dev_id = repo->dev_id;
  176. p->dev.d_region = &p->d_region;
  177. p->dev.m_region = &p->m_region;
  178. result = ps3_repository_find_interrupt(repo,
  179. interrupt_type, &p->dev.interrupt_id);
  180. if (result) {
  181. pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
  182. __func__, __LINE__);
  183. goto fail_find_interrupt;
  184. }
  185. result = ps3_repository_find_reg(repo, reg_type,
  186. &bus_addr, &len);
  187. if (result) {
  188. pr_debug("%s:%d ps3_repository_find_reg failed\n",
  189. __func__, __LINE__);
  190. goto fail_find_reg;
  191. }
  192. result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
  193. PS3_DMA_INTERNAL, NULL, 0);
  194. if (result) {
  195. pr_debug("%s:%d ps3_dma_region_init failed\n",
  196. __func__, __LINE__);
  197. goto fail_dma_init;
  198. }
  199. result = ps3_mmio_region_init(&p->dev, p->dev.m_region, bus_addr, len,
  200. PS3_MMIO_4K);
  201. if (result) {
  202. pr_debug("%s:%d ps3_mmio_region_init failed\n",
  203. __func__, __LINE__);
  204. goto fail_mmio_init;
  205. }
  206. result = ps3_system_bus_device_register(&p->dev);
  207. if (result) {
  208. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  209. __func__, __LINE__);
  210. goto fail_device_register;
  211. }
  212. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  213. return result;
  214. fail_device_register:
  215. fail_mmio_init:
  216. fail_dma_init:
  217. fail_find_reg:
  218. fail_find_interrupt:
  219. kfree(p);
  220. fail_malloc:
  221. pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
  222. return result;
  223. }
  224. static int __init ps3_setup_ehci_device(
  225. const struct ps3_repository_device *repo)
  226. {
  227. return ps3_setup_uhc_device(repo, PS3_MATCH_ID_EHCI,
  228. PS3_INTERRUPT_TYPE_SB_EHCI, PS3_REG_TYPE_SB_EHCI);
  229. }
  230. static int __init ps3_setup_ohci_device(
  231. const struct ps3_repository_device *repo)
  232. {
  233. return ps3_setup_uhc_device(repo, PS3_MATCH_ID_OHCI,
  234. PS3_INTERRUPT_TYPE_SB_OHCI, PS3_REG_TYPE_SB_OHCI);
  235. }
  236. static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,
  237. unsigned int port_number)
  238. {
  239. int result;
  240. struct layout {
  241. struct ps3_system_bus_device dev;
  242. } *p;
  243. pr_debug(" -> %s:%d: match_id %u, port %u\n", __func__, __LINE__,
  244. match_id, port_number);
  245. p = kzalloc(sizeof(struct layout), GFP_KERNEL);
  246. if (!p)
  247. return -ENOMEM;
  248. p->dev.match_id = match_id;
  249. p->dev.dev_type = PS3_DEVICE_TYPE_VUART;
  250. p->dev.port_number = port_number;
  251. result = ps3_system_bus_device_register(&p->dev);
  252. if (result) {
  253. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  254. __func__, __LINE__);
  255. goto fail_device_register;
  256. }
  257. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  258. return 0;
  259. fail_device_register:
  260. kfree(p);
  261. pr_debug(" <- %s:%d fail\n", __func__, __LINE__);
  262. return result;
  263. }
  264. static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
  265. enum ps3_match_id match_id)
  266. {
  267. int result;
  268. struct ps3_storage_device *p;
  269. u64 port, blk_size, num_blocks;
  270. unsigned int num_regions, i;
  271. pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
  272. result = ps3_repository_read_stor_dev_info(repo->bus_index,
  273. repo->dev_index, &port,
  274. &blk_size, &num_blocks,
  275. &num_regions);
  276. if (result) {
  277. printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
  278. __func__, __LINE__, result);
  279. return -ENODEV;
  280. }
  281. pr_debug("%s:%u: (%u:%u:%u): port %llu blk_size %llu num_blocks %llu "
  282. "num_regions %u\n", __func__, __LINE__, repo->bus_index,
  283. repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
  284. num_regions);
  285. p = kzalloc(sizeof(struct ps3_storage_device) +
  286. num_regions * sizeof(struct ps3_storage_region),
  287. GFP_KERNEL);
  288. if (!p) {
  289. result = -ENOMEM;
  290. goto fail_malloc;
  291. }
  292. p->sbd.match_id = match_id;
  293. p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
  294. p->sbd.bus_id = repo->bus_id;
  295. p->sbd.dev_id = repo->dev_id;
  296. p->sbd.d_region = &p->dma_region;
  297. p->blk_size = blk_size;
  298. p->num_regions = num_regions;
  299. result = ps3_repository_find_interrupt(repo,
  300. PS3_INTERRUPT_TYPE_EVENT_PORT,
  301. &p->sbd.interrupt_id);
  302. if (result) {
  303. printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
  304. __LINE__, result);
  305. result = -ENODEV;
  306. goto fail_find_interrupt;
  307. }
  308. for (i = 0; i < num_regions; i++) {
  309. unsigned int id;
  310. u64 start, size;
  311. result = ps3_repository_read_stor_dev_region(repo->bus_index,
  312. repo->dev_index,
  313. i, &id, &start,
  314. &size);
  315. if (result) {
  316. printk(KERN_ERR
  317. "%s:%u: read_stor_dev_region failed %d\n",
  318. __func__, __LINE__, result);
  319. result = -ENODEV;
  320. goto fail_read_region;
  321. }
  322. pr_debug("%s:%u: region %u: id %u start %llu size %llu\n",
  323. __func__, __LINE__, i, id, start, size);
  324. p->regions[i].id = id;
  325. p->regions[i].start = start;
  326. p->regions[i].size = size;
  327. }
  328. result = ps3_system_bus_device_register(&p->sbd);
  329. if (result) {
  330. pr_debug("%s:%u ps3_system_bus_device_register failed\n",
  331. __func__, __LINE__);
  332. goto fail_device_register;
  333. }
  334. pr_debug(" <- %s:%u\n", __func__, __LINE__);
  335. return 0;
  336. fail_device_register:
  337. fail_read_region:
  338. fail_find_interrupt:
  339. kfree(p);
  340. fail_malloc:
  341. pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
  342. return result;
  343. }
  344. static int __init ps3_register_vuart_devices(void)
  345. {
  346. int result;
  347. unsigned int port_number;
  348. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  349. result = ps3_repository_read_vuart_av_port(&port_number);
  350. if (result)
  351. port_number = 0; /* av default */
  352. result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
  353. WARN_ON(result);
  354. result = ps3_repository_read_vuart_sysmgr_port(&port_number);
  355. if (result)
  356. port_number = 2; /* sysmgr default */
  357. result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
  358. port_number);
  359. WARN_ON(result);
  360. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  361. return result;
  362. }
  363. static int __init ps3_register_sound_devices(void)
  364. {
  365. int result;
  366. struct layout {
  367. struct ps3_system_bus_device dev;
  368. struct ps3_dma_region d_region;
  369. struct ps3_mmio_region m_region;
  370. } *p;
  371. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  372. p = kzalloc(sizeof(*p), GFP_KERNEL);
  373. if (!p)
  374. return -ENOMEM;
  375. p->dev.match_id = PS3_MATCH_ID_SOUND;
  376. p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
  377. p->dev.d_region = &p->d_region;
  378. p->dev.m_region = &p->m_region;
  379. result = ps3_system_bus_device_register(&p->dev);
  380. if (result) {
  381. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  382. __func__, __LINE__);
  383. goto fail_device_register;
  384. }
  385. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  386. return 0;
  387. fail_device_register:
  388. kfree(p);
  389. pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
  390. return result;
  391. }
  392. static int __init ps3_register_graphics_devices(void)
  393. {
  394. int result;
  395. struct layout {
  396. struct ps3_system_bus_device dev;
  397. } *p;
  398. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  399. p = kzalloc(sizeof(struct layout), GFP_KERNEL);
  400. if (!p)
  401. return -ENOMEM;
  402. p->dev.match_id = PS3_MATCH_ID_GPU;
  403. p->dev.match_sub_id = PS3_MATCH_SUB_ID_GPU_FB;
  404. p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
  405. result = ps3_system_bus_device_register(&p->dev);
  406. if (result) {
  407. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  408. __func__, __LINE__);
  409. goto fail_device_register;
  410. }
  411. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  412. return 0;
  413. fail_device_register:
  414. kfree(p);
  415. pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
  416. return result;
  417. }
  418. static int __init ps3_register_ramdisk_device(void)
  419. {
  420. int result;
  421. struct layout {
  422. struct ps3_system_bus_device dev;
  423. } *p;
  424. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  425. p = kzalloc(sizeof(struct layout), GFP_KERNEL);
  426. if (!p)
  427. return -ENOMEM;
  428. p->dev.match_id = PS3_MATCH_ID_GPU;
  429. p->dev.match_sub_id = PS3_MATCH_SUB_ID_GPU_RAMDISK;
  430. p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
  431. result = ps3_system_bus_device_register(&p->dev);
  432. if (result) {
  433. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  434. __func__, __LINE__);
  435. goto fail_device_register;
  436. }
  437. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  438. return 0;
  439. fail_device_register:
  440. kfree(p);
  441. pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
  442. return result;
  443. }
  444. /**
  445. * ps3_setup_dynamic_device - Setup a dynamic device from the repository
  446. */
  447. static int ps3_setup_dynamic_device(const struct ps3_repository_device *repo)
  448. {
  449. int result;
  450. switch (repo->dev_type) {
  451. case PS3_DEV_TYPE_STOR_DISK:
  452. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
  453. /* Some devices are not accessible from the Other OS lpar. */
  454. if (result == -ENODEV) {
  455. result = 0;
  456. pr_debug("%s:%u: not accessible\n", __func__,
  457. __LINE__);
  458. }
  459. if (result)
  460. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  461. __func__, __LINE__);
  462. break;
  463. case PS3_DEV_TYPE_STOR_ROM:
  464. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
  465. if (result)
  466. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  467. __func__, __LINE__);
  468. break;
  469. case PS3_DEV_TYPE_STOR_FLASH:
  470. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
  471. if (result)
  472. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  473. __func__, __LINE__);
  474. break;
  475. default:
  476. result = 0;
  477. pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
  478. repo->dev_type);
  479. }
  480. return result;
  481. }
  482. /**
  483. * ps3_setup_static_device - Setup a static device from the repository
  484. */
  485. static int __init ps3_setup_static_device(const struct ps3_repository_device *repo)
  486. {
  487. int result;
  488. switch (repo->dev_type) {
  489. case PS3_DEV_TYPE_SB_GELIC:
  490. result = ps3_setup_gelic_device(repo);
  491. if (result) {
  492. pr_debug("%s:%d ps3_setup_gelic_device failed\n",
  493. __func__, __LINE__);
  494. }
  495. break;
  496. case PS3_DEV_TYPE_SB_USB:
  497. /* Each USB device has both an EHCI and an OHCI HC */
  498. result = ps3_setup_ehci_device(repo);
  499. if (result) {
  500. pr_debug("%s:%d ps3_setup_ehci_device failed\n",
  501. __func__, __LINE__);
  502. }
  503. result = ps3_setup_ohci_device(repo);
  504. if (result) {
  505. pr_debug("%s:%d ps3_setup_ohci_device failed\n",
  506. __func__, __LINE__);
  507. }
  508. break;
  509. default:
  510. return ps3_setup_dynamic_device(repo);
  511. }
  512. return result;
  513. }
  514. static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
  515. {
  516. struct ps3_repository_device repo;
  517. int res;
  518. unsigned int retries;
  519. unsigned long rem;
  520. /*
  521. * On some firmware versions (e.g. 1.90), the device may not show up
  522. * in the repository immediately
  523. */
  524. for (retries = 0; retries < 10; retries++) {
  525. res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
  526. if (!res)
  527. goto found;
  528. rem = msleep_interruptible(100);
  529. if (rem)
  530. break;
  531. }
  532. pr_warning("%s:%u: device %llu:%llu not found\n", __func__, __LINE__,
  533. bus_id, dev_id);
  534. return;
  535. found:
  536. if (retries)
  537. pr_debug("%s:%u: device %llu:%llu found after %u retries\n",
  538. __func__, __LINE__, bus_id, dev_id, retries);
  539. ps3_setup_dynamic_device(&repo);
  540. return;
  541. }
  542. #define PS3_NOTIFICATION_DEV_ID ULONG_MAX
  543. #define PS3_NOTIFICATION_INTERRUPT_ID 0
  544. struct ps3_notification_device {
  545. struct ps3_system_bus_device sbd;
  546. spinlock_t lock;
  547. u64 tag;
  548. u64 lv1_status;
  549. struct completion done;
  550. };
  551. enum ps3_notify_type {
  552. notify_device_ready = 0,
  553. notify_region_probe = 1,
  554. notify_region_update = 2,
  555. };
  556. struct ps3_notify_cmd {
  557. u64 operation_code; /* must be zero */
  558. u64 event_mask; /* OR of 1UL << enum ps3_notify_type */
  559. };
  560. struct ps3_notify_event {
  561. u64 event_type; /* enum ps3_notify_type */
  562. u64 bus_id;
  563. u64 dev_id;
  564. u64 dev_type;
  565. u64 dev_port;
  566. };
  567. static irqreturn_t ps3_notification_interrupt(int irq, void *data)
  568. {
  569. struct ps3_notification_device *dev = data;
  570. int res;
  571. u64 tag, status;
  572. spin_lock(&dev->lock);
  573. res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
  574. &status);
  575. if (tag != dev->tag)
  576. pr_err("%s:%u: tag mismatch, got %llx, expected %llx\n",
  577. __func__, __LINE__, tag, dev->tag);
  578. if (res) {
  579. pr_err("%s:%u: res %d status 0x%llx\n", __func__, __LINE__, res,
  580. status);
  581. } else {
  582. pr_debug("%s:%u: completed, status 0x%llx\n", __func__,
  583. __LINE__, status);
  584. dev->lv1_status = status;
  585. complete(&dev->done);
  586. }
  587. spin_unlock(&dev->lock);
  588. return IRQ_HANDLED;
  589. }
  590. static int ps3_notification_read_write(struct ps3_notification_device *dev,
  591. u64 lpar, int write)
  592. {
  593. const char *op = write ? "write" : "read";
  594. unsigned long flags;
  595. int res;
  596. init_completion(&dev->done);
  597. spin_lock_irqsave(&dev->lock, flags);
  598. res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
  599. &dev->tag)
  600. : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
  601. &dev->tag);
  602. spin_unlock_irqrestore(&dev->lock, flags);
  603. if (res) {
  604. pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
  605. return -EPERM;
  606. }
  607. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  608. res = wait_event_interruptible(dev->done.wait,
  609. dev->done.done || kthread_should_stop());
  610. if (kthread_should_stop())
  611. res = -EINTR;
  612. if (res) {
  613. pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
  614. return res;
  615. }
  616. if (dev->lv1_status) {
  617. pr_err("%s:%u: %s not completed, status 0x%llx\n", __func__,
  618. __LINE__, op, dev->lv1_status);
  619. return -EIO;
  620. }
  621. pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
  622. return 0;
  623. }
  624. static struct task_struct *probe_task;
  625. /**
  626. * ps3_probe_thread - Background repository probing at system startup.
  627. *
  628. * This implementation only supports background probing on a single bus.
  629. * It uses the hypervisor's storage device notification mechanism to wait until
  630. * a storage device is ready. The device notification mechanism uses a
  631. * pseudo device to asynchronously notify the guest when storage devices become
  632. * ready. The notification device has a block size of 512 bytes.
  633. */
  634. static int ps3_probe_thread(void *data)
  635. {
  636. struct ps3_notification_device dev;
  637. int res;
  638. unsigned int irq;
  639. u64 lpar;
  640. void *buf;
  641. struct ps3_notify_cmd *notify_cmd;
  642. struct ps3_notify_event *notify_event;
  643. pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
  644. buf = kzalloc(512, GFP_KERNEL);
  645. if (!buf)
  646. return -ENOMEM;
  647. lpar = ps3_mm_phys_to_lpar(__pa(buf));
  648. notify_cmd = buf;
  649. notify_event = buf;
  650. /* dummy system bus device */
  651. dev.sbd.bus_id = (u64)data;
  652. dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
  653. dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
  654. res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
  655. if (res) {
  656. pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
  657. __LINE__, ps3_result(res));
  658. goto fail_free;
  659. }
  660. res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
  661. &irq);
  662. if (res) {
  663. pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
  664. __func__, __LINE__, res);
  665. goto fail_close_device;
  666. }
  667. spin_lock_init(&dev.lock);
  668. res = request_irq(irq, ps3_notification_interrupt, 0,
  669. "ps3_notification", &dev);
  670. if (res) {
  671. pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
  672. res);
  673. goto fail_sb_event_receive_port_destroy;
  674. }
  675. /* Setup and write the request for device notification. */
  676. notify_cmd->operation_code = 0; /* must be zero */
  677. notify_cmd->event_mask = 1UL << notify_region_probe;
  678. res = ps3_notification_read_write(&dev, lpar, 1);
  679. if (res)
  680. goto fail_free_irq;
  681. /* Loop here processing the requested notification events. */
  682. do {
  683. try_to_freeze();
  684. memset(notify_event, 0, sizeof(*notify_event));
  685. res = ps3_notification_read_write(&dev, lpar, 0);
  686. if (res)
  687. break;
  688. pr_debug("%s:%u: notify event type 0x%llx bus id %llu dev id %llu"
  689. " type %llu port %llu\n", __func__, __LINE__,
  690. notify_event->event_type, notify_event->bus_id,
  691. notify_event->dev_id, notify_event->dev_type,
  692. notify_event->dev_port);
  693. if (notify_event->event_type != notify_region_probe ||
  694. notify_event->bus_id != dev.sbd.bus_id) {
  695. pr_warning("%s:%u: bad notify_event: event %llu, "
  696. "dev_id %llu, dev_type %llu\n",
  697. __func__, __LINE__, notify_event->event_type,
  698. notify_event->dev_id,
  699. notify_event->dev_type);
  700. continue;
  701. }
  702. ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
  703. } while (!kthread_should_stop());
  704. fail_free_irq:
  705. free_irq(irq, &dev);
  706. fail_sb_event_receive_port_destroy:
  707. ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
  708. fail_close_device:
  709. lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
  710. fail_free:
  711. kfree(buf);
  712. probe_task = NULL;
  713. pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
  714. return 0;
  715. }
  716. /**
  717. * ps3_stop_probe_thread - Stops the background probe thread.
  718. *
  719. */
  720. static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
  721. void *data)
  722. {
  723. if (probe_task)
  724. kthread_stop(probe_task);
  725. return 0;
  726. }
  727. static struct notifier_block nb = {
  728. .notifier_call = ps3_stop_probe_thread
  729. };
  730. /**
  731. * ps3_start_probe_thread - Starts the background probe thread.
  732. *
  733. */
  734. static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
  735. {
  736. int result;
  737. struct task_struct *task;
  738. struct ps3_repository_device repo;
  739. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  740. memset(&repo, 0, sizeof(repo));
  741. repo.bus_type = bus_type;
  742. result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
  743. if (result) {
  744. printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
  745. return -ENODEV;
  746. }
  747. result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
  748. if (result) {
  749. printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
  750. result);
  751. return -ENODEV;
  752. }
  753. task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
  754. "ps3-probe-%u", bus_type);
  755. if (IS_ERR(task)) {
  756. result = PTR_ERR(task);
  757. printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
  758. result);
  759. return result;
  760. }
  761. probe_task = task;
  762. register_reboot_notifier(&nb);
  763. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  764. return 0;
  765. }
  766. /**
  767. * ps3_register_devices - Probe the system and register devices found.
  768. *
  769. * A device_initcall() routine.
  770. */
  771. static int __init ps3_register_devices(void)
  772. {
  773. int result;
  774. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  775. return -ENODEV;
  776. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  777. /* ps3_repository_dump_bus_info(); */
  778. result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
  779. ps3_register_vuart_devices();
  780. ps3_register_graphics_devices();
  781. ps3_repository_find_devices(PS3_BUS_TYPE_SB, ps3_setup_static_device);
  782. ps3_register_sound_devices();
  783. ps3_register_lpm_devices();
  784. ps3_register_ramdisk_device();
  785. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  786. return 0;
  787. }
  788. device_initcall(ps3_register_devices);