device-init.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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 %lu, rights %lu(%lxh)\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. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  255. return result;
  256. }
  257. static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
  258. enum ps3_match_id match_id)
  259. {
  260. int result;
  261. struct ps3_storage_device *p;
  262. u64 port, blk_size, num_blocks;
  263. unsigned int num_regions, i;
  264. pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
  265. result = ps3_repository_read_stor_dev_info(repo->bus_index,
  266. repo->dev_index, &port,
  267. &blk_size, &num_blocks,
  268. &num_regions);
  269. if (result) {
  270. printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
  271. __func__, __LINE__, result);
  272. return -ENODEV;
  273. }
  274. pr_debug("%s:%u: (%u:%u:%u): port %lu blk_size %lu num_blocks %lu "
  275. "num_regions %u\n", __func__, __LINE__, repo->bus_index,
  276. repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
  277. num_regions);
  278. p = kzalloc(sizeof(struct ps3_storage_device) +
  279. num_regions * sizeof(struct ps3_storage_region),
  280. GFP_KERNEL);
  281. if (!p) {
  282. result = -ENOMEM;
  283. goto fail_malloc;
  284. }
  285. p->sbd.match_id = match_id;
  286. p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
  287. p->sbd.bus_id = repo->bus_id;
  288. p->sbd.dev_id = repo->dev_id;
  289. p->sbd.d_region = &p->dma_region;
  290. p->blk_size = blk_size;
  291. p->num_regions = num_regions;
  292. result = ps3_repository_find_interrupt(repo,
  293. PS3_INTERRUPT_TYPE_EVENT_PORT,
  294. &p->sbd.interrupt_id);
  295. if (result) {
  296. printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
  297. __LINE__, result);
  298. result = -ENODEV;
  299. goto fail_find_interrupt;
  300. }
  301. for (i = 0; i < num_regions; i++) {
  302. unsigned int id;
  303. u64 start, size;
  304. result = ps3_repository_read_stor_dev_region(repo->bus_index,
  305. repo->dev_index,
  306. i, &id, &start,
  307. &size);
  308. if (result) {
  309. printk(KERN_ERR
  310. "%s:%u: read_stor_dev_region failed %d\n",
  311. __func__, __LINE__, result);
  312. result = -ENODEV;
  313. goto fail_read_region;
  314. }
  315. pr_debug("%s:%u: region %u: id %u start %lu size %lu\n",
  316. __func__, __LINE__, i, id, start, size);
  317. p->regions[i].id = id;
  318. p->regions[i].start = start;
  319. p->regions[i].size = size;
  320. }
  321. result = ps3_system_bus_device_register(&p->sbd);
  322. if (result) {
  323. pr_debug("%s:%u ps3_system_bus_device_register failed\n",
  324. __func__, __LINE__);
  325. goto fail_device_register;
  326. }
  327. pr_debug(" <- %s:%u\n", __func__, __LINE__);
  328. return 0;
  329. fail_device_register:
  330. fail_read_region:
  331. fail_find_interrupt:
  332. kfree(p);
  333. fail_malloc:
  334. pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
  335. return result;
  336. }
  337. static int __init ps3_register_vuart_devices(void)
  338. {
  339. int result;
  340. unsigned int port_number;
  341. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  342. result = ps3_repository_read_vuart_av_port(&port_number);
  343. if (result)
  344. port_number = 0; /* av default */
  345. result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
  346. WARN_ON(result);
  347. result = ps3_repository_read_vuart_sysmgr_port(&port_number);
  348. if (result)
  349. port_number = 2; /* sysmgr default */
  350. result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
  351. port_number);
  352. WARN_ON(result);
  353. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  354. return result;
  355. }
  356. static int __init ps3_register_sound_devices(void)
  357. {
  358. int result;
  359. struct layout {
  360. struct ps3_system_bus_device dev;
  361. struct ps3_dma_region d_region;
  362. struct ps3_mmio_region m_region;
  363. } *p;
  364. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  365. p = kzalloc(sizeof(*p), GFP_KERNEL);
  366. if (!p)
  367. return -ENOMEM;
  368. p->dev.match_id = PS3_MATCH_ID_SOUND;
  369. p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
  370. p->dev.d_region = &p->d_region;
  371. p->dev.m_region = &p->m_region;
  372. result = ps3_system_bus_device_register(&p->dev);
  373. if (result)
  374. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  375. __func__, __LINE__);
  376. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  377. return result;
  378. }
  379. static int __init ps3_register_graphics_devices(void)
  380. {
  381. int result;
  382. struct layout {
  383. struct ps3_system_bus_device dev;
  384. } *p;
  385. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  386. p = kzalloc(sizeof(struct layout), GFP_KERNEL);
  387. if (!p)
  388. return -ENOMEM;
  389. p->dev.match_id = PS3_MATCH_ID_GRAPHICS;
  390. p->dev.match_sub_id = PS3_MATCH_SUB_ID_FB;
  391. p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
  392. result = ps3_system_bus_device_register(&p->dev);
  393. if (result)
  394. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  395. __func__, __LINE__);
  396. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  397. return result;
  398. }
  399. /**
  400. * ps3_setup_dynamic_device - Setup a dynamic device from the repository
  401. */
  402. static int ps3_setup_dynamic_device(const struct ps3_repository_device *repo)
  403. {
  404. int result;
  405. switch (repo->dev_type) {
  406. case PS3_DEV_TYPE_STOR_DISK:
  407. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
  408. /* Some devices are not accessable from the Other OS lpar. */
  409. if (result == -ENODEV) {
  410. result = 0;
  411. pr_debug("%s:%u: not accessable\n", __func__,
  412. __LINE__);
  413. }
  414. if (result)
  415. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  416. __func__, __LINE__);
  417. break;
  418. case PS3_DEV_TYPE_STOR_ROM:
  419. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
  420. if (result)
  421. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  422. __func__, __LINE__);
  423. break;
  424. case PS3_DEV_TYPE_STOR_FLASH:
  425. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
  426. if (result)
  427. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  428. __func__, __LINE__);
  429. break;
  430. default:
  431. result = 0;
  432. pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
  433. repo->dev_type);
  434. }
  435. return result;
  436. }
  437. /**
  438. * ps3_setup_static_device - Setup a static device from the repository
  439. */
  440. static int __init ps3_setup_static_device(const struct ps3_repository_device *repo)
  441. {
  442. int result;
  443. switch (repo->dev_type) {
  444. case PS3_DEV_TYPE_SB_GELIC:
  445. result = ps3_setup_gelic_device(repo);
  446. if (result) {
  447. pr_debug("%s:%d ps3_setup_gelic_device failed\n",
  448. __func__, __LINE__);
  449. }
  450. break;
  451. case PS3_DEV_TYPE_SB_USB:
  452. /* Each USB device has both an EHCI and an OHCI HC */
  453. result = ps3_setup_ehci_device(repo);
  454. if (result) {
  455. pr_debug("%s:%d ps3_setup_ehci_device failed\n",
  456. __func__, __LINE__);
  457. }
  458. result = ps3_setup_ohci_device(repo);
  459. if (result) {
  460. pr_debug("%s:%d ps3_setup_ohci_device failed\n",
  461. __func__, __LINE__);
  462. }
  463. break;
  464. default:
  465. return ps3_setup_dynamic_device(repo);
  466. }
  467. return result;
  468. }
  469. static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
  470. {
  471. struct ps3_repository_device repo;
  472. int res;
  473. unsigned int retries;
  474. unsigned long rem;
  475. /*
  476. * On some firmware versions (e.g. 1.90), the device may not show up
  477. * in the repository immediately
  478. */
  479. for (retries = 0; retries < 10; retries++) {
  480. res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
  481. if (!res)
  482. goto found;
  483. rem = msleep_interruptible(100);
  484. if (rem)
  485. break;
  486. }
  487. pr_warning("%s:%u: device %lu:%lu not found\n", __func__, __LINE__,
  488. bus_id, dev_id);
  489. return;
  490. found:
  491. if (retries)
  492. pr_debug("%s:%u: device %lu:%lu found after %u retries\n",
  493. __func__, __LINE__, bus_id, dev_id, retries);
  494. ps3_setup_dynamic_device(&repo);
  495. return;
  496. }
  497. #define PS3_NOTIFICATION_DEV_ID ULONG_MAX
  498. #define PS3_NOTIFICATION_INTERRUPT_ID 0
  499. struct ps3_notification_device {
  500. struct ps3_system_bus_device sbd;
  501. spinlock_t lock;
  502. u64 tag;
  503. u64 lv1_status;
  504. struct completion done;
  505. };
  506. enum ps3_notify_type {
  507. notify_device_ready = 0,
  508. notify_region_probe = 1,
  509. notify_region_update = 2,
  510. };
  511. struct ps3_notify_cmd {
  512. u64 operation_code; /* must be zero */
  513. u64 event_mask; /* OR of 1UL << enum ps3_notify_type */
  514. };
  515. struct ps3_notify_event {
  516. u64 event_type; /* enum ps3_notify_type */
  517. u64 bus_id;
  518. u64 dev_id;
  519. u64 dev_type;
  520. u64 dev_port;
  521. };
  522. static irqreturn_t ps3_notification_interrupt(int irq, void *data)
  523. {
  524. struct ps3_notification_device *dev = data;
  525. int res;
  526. u64 tag, status;
  527. spin_lock(&dev->lock);
  528. res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
  529. &status);
  530. if (tag != dev->tag)
  531. pr_err("%s:%u: tag mismatch, got %lx, expected %lx\n",
  532. __func__, __LINE__, tag, dev->tag);
  533. if (res) {
  534. pr_err("%s:%u: res %d status 0x%lx\n", __func__, __LINE__, res,
  535. status);
  536. } else {
  537. pr_debug("%s:%u: completed, status 0x%lx\n", __func__,
  538. __LINE__, status);
  539. dev->lv1_status = status;
  540. complete(&dev->done);
  541. }
  542. spin_unlock(&dev->lock);
  543. return IRQ_HANDLED;
  544. }
  545. static int ps3_notification_read_write(struct ps3_notification_device *dev,
  546. u64 lpar, int write)
  547. {
  548. const char *op = write ? "write" : "read";
  549. unsigned long flags;
  550. int res;
  551. init_completion(&dev->done);
  552. spin_lock_irqsave(&dev->lock, flags);
  553. res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
  554. &dev->tag)
  555. : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
  556. &dev->tag);
  557. spin_unlock_irqrestore(&dev->lock, flags);
  558. if (res) {
  559. pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
  560. return -EPERM;
  561. }
  562. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  563. res = wait_event_interruptible(dev->done.wait,
  564. dev->done.done || kthread_should_stop());
  565. if (kthread_should_stop())
  566. res = -EINTR;
  567. if (res) {
  568. pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
  569. return res;
  570. }
  571. if (dev->lv1_status) {
  572. pr_err("%s:%u: %s not completed, status 0x%lx\n", __func__,
  573. __LINE__, op, dev->lv1_status);
  574. return -EIO;
  575. }
  576. pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
  577. return 0;
  578. }
  579. static struct task_struct *probe_task;
  580. /**
  581. * ps3_probe_thread - Background repository probing at system startup.
  582. *
  583. * This implementation only supports background probing on a single bus.
  584. * It uses the hypervisor's storage device notification mechanism to wait until
  585. * a storage device is ready. The device notification mechanism uses a
  586. * pseudo device to asynchronously notify the guest when storage devices become
  587. * ready. The notification device has a block size of 512 bytes.
  588. */
  589. static int ps3_probe_thread(void *data)
  590. {
  591. struct ps3_notification_device dev;
  592. int res;
  593. unsigned int irq;
  594. u64 lpar;
  595. void *buf;
  596. struct ps3_notify_cmd *notify_cmd;
  597. struct ps3_notify_event *notify_event;
  598. pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
  599. buf = kzalloc(512, GFP_KERNEL);
  600. if (!buf)
  601. return -ENOMEM;
  602. lpar = ps3_mm_phys_to_lpar(__pa(buf));
  603. notify_cmd = buf;
  604. notify_event = buf;
  605. /* dummy system bus device */
  606. dev.sbd.bus_id = (u64)data;
  607. dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
  608. dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
  609. res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
  610. if (res) {
  611. pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
  612. __LINE__, ps3_result(res));
  613. goto fail_free;
  614. }
  615. res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
  616. &irq);
  617. if (res) {
  618. pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
  619. __func__, __LINE__, res);
  620. goto fail_close_device;
  621. }
  622. spin_lock_init(&dev.lock);
  623. res = request_irq(irq, ps3_notification_interrupt, IRQF_DISABLED,
  624. "ps3_notification", &dev);
  625. if (res) {
  626. pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
  627. res);
  628. goto fail_sb_event_receive_port_destroy;
  629. }
  630. /* Setup and write the request for device notification. */
  631. notify_cmd->operation_code = 0; /* must be zero */
  632. notify_cmd->event_mask = 1UL << notify_region_probe;
  633. res = ps3_notification_read_write(&dev, lpar, 1);
  634. if (res)
  635. goto fail_free_irq;
  636. /* Loop here processing the requested notification events. */
  637. do {
  638. try_to_freeze();
  639. memset(notify_event, 0, sizeof(*notify_event));
  640. res = ps3_notification_read_write(&dev, lpar, 0);
  641. if (res)
  642. break;
  643. pr_debug("%s:%u: notify event type 0x%lx bus id %lu dev id %lu"
  644. " type %lu port %lu\n", __func__, __LINE__,
  645. notify_event->event_type, notify_event->bus_id,
  646. notify_event->dev_id, notify_event->dev_type,
  647. notify_event->dev_port);
  648. if (notify_event->event_type != notify_region_probe ||
  649. notify_event->bus_id != dev.sbd.bus_id) {
  650. pr_warning("%s:%u: bad notify_event: event %lu, "
  651. "dev_id %lu, dev_type %lu\n",
  652. __func__, __LINE__, notify_event->event_type,
  653. notify_event->dev_id,
  654. notify_event->dev_type);
  655. continue;
  656. }
  657. ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
  658. } while (!kthread_should_stop());
  659. fail_free_irq:
  660. free_irq(irq, &dev);
  661. fail_sb_event_receive_port_destroy:
  662. ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
  663. fail_close_device:
  664. lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
  665. fail_free:
  666. kfree(buf);
  667. probe_task = NULL;
  668. pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
  669. return 0;
  670. }
  671. /**
  672. * ps3_stop_probe_thread - Stops the background probe thread.
  673. *
  674. */
  675. static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
  676. void *data)
  677. {
  678. if (probe_task)
  679. kthread_stop(probe_task);
  680. return 0;
  681. }
  682. static struct notifier_block nb = {
  683. .notifier_call = ps3_stop_probe_thread
  684. };
  685. /**
  686. * ps3_start_probe_thread - Starts the background probe thread.
  687. *
  688. */
  689. static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
  690. {
  691. int result;
  692. struct task_struct *task;
  693. struct ps3_repository_device repo;
  694. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  695. memset(&repo, 0, sizeof(repo));
  696. repo.bus_type = bus_type;
  697. result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
  698. if (result) {
  699. printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
  700. return -ENODEV;
  701. }
  702. result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
  703. if (result) {
  704. printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
  705. result);
  706. return -ENODEV;
  707. }
  708. task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
  709. "ps3-probe-%u", bus_type);
  710. if (IS_ERR(task)) {
  711. result = PTR_ERR(task);
  712. printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
  713. result);
  714. return result;
  715. }
  716. probe_task = task;
  717. register_reboot_notifier(&nb);
  718. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  719. return 0;
  720. }
  721. /**
  722. * ps3_register_devices - Probe the system and register devices found.
  723. *
  724. * A device_initcall() routine.
  725. */
  726. static int __init ps3_register_devices(void)
  727. {
  728. int result;
  729. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  730. return -ENODEV;
  731. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  732. /* ps3_repository_dump_bus_info(); */
  733. result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
  734. ps3_register_vuart_devices();
  735. ps3_register_graphics_devices();
  736. ps3_repository_find_devices(PS3_BUS_TYPE_SB, ps3_setup_static_device);
  737. ps3_register_sound_devices();
  738. ps3_register_lpm_devices();
  739. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  740. return 0;
  741. }
  742. device_initcall(ps3_register_devices);