device-init.c 23 KB

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