device-init.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. 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 %lu blk_size %lu num_blocks %lu "
  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 %lu size %lu\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. /**
  418. * ps3_setup_dynamic_device - Setup a dynamic device from the repository
  419. */
  420. static int ps3_setup_dynamic_device(const struct ps3_repository_device *repo)
  421. {
  422. int result;
  423. switch (repo->dev_type) {
  424. case PS3_DEV_TYPE_STOR_DISK:
  425. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
  426. /* Some devices are not accessable from the Other OS lpar. */
  427. if (result == -ENODEV) {
  428. result = 0;
  429. pr_debug("%s:%u: not accessable\n", __func__,
  430. __LINE__);
  431. }
  432. if (result)
  433. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  434. __func__, __LINE__);
  435. break;
  436. case PS3_DEV_TYPE_STOR_ROM:
  437. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
  438. if (result)
  439. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  440. __func__, __LINE__);
  441. break;
  442. case PS3_DEV_TYPE_STOR_FLASH:
  443. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
  444. if (result)
  445. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  446. __func__, __LINE__);
  447. break;
  448. default:
  449. result = 0;
  450. pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
  451. repo->dev_type);
  452. }
  453. return result;
  454. }
  455. /**
  456. * ps3_setup_static_device - Setup a static device from the repository
  457. */
  458. static int __init ps3_setup_static_device(const struct ps3_repository_device *repo)
  459. {
  460. int result;
  461. switch (repo->dev_type) {
  462. case PS3_DEV_TYPE_SB_GELIC:
  463. result = ps3_setup_gelic_device(repo);
  464. if (result) {
  465. pr_debug("%s:%d ps3_setup_gelic_device failed\n",
  466. __func__, __LINE__);
  467. }
  468. break;
  469. case PS3_DEV_TYPE_SB_USB:
  470. /* Each USB device has both an EHCI and an OHCI HC */
  471. result = ps3_setup_ehci_device(repo);
  472. if (result) {
  473. pr_debug("%s:%d ps3_setup_ehci_device failed\n",
  474. __func__, __LINE__);
  475. }
  476. result = ps3_setup_ohci_device(repo);
  477. if (result) {
  478. pr_debug("%s:%d ps3_setup_ohci_device failed\n",
  479. __func__, __LINE__);
  480. }
  481. break;
  482. default:
  483. return ps3_setup_dynamic_device(repo);
  484. }
  485. return result;
  486. }
  487. static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
  488. {
  489. struct ps3_repository_device repo;
  490. int res;
  491. unsigned int retries;
  492. unsigned long rem;
  493. /*
  494. * On some firmware versions (e.g. 1.90), the device may not show up
  495. * in the repository immediately
  496. */
  497. for (retries = 0; retries < 10; retries++) {
  498. res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
  499. if (!res)
  500. goto found;
  501. rem = msleep_interruptible(100);
  502. if (rem)
  503. break;
  504. }
  505. pr_warning("%s:%u: device %lu:%lu not found\n", __func__, __LINE__,
  506. bus_id, dev_id);
  507. return;
  508. found:
  509. if (retries)
  510. pr_debug("%s:%u: device %lu:%lu found after %u retries\n",
  511. __func__, __LINE__, bus_id, dev_id, retries);
  512. ps3_setup_dynamic_device(&repo);
  513. return;
  514. }
  515. #define PS3_NOTIFICATION_DEV_ID ULONG_MAX
  516. #define PS3_NOTIFICATION_INTERRUPT_ID 0
  517. struct ps3_notification_device {
  518. struct ps3_system_bus_device sbd;
  519. spinlock_t lock;
  520. u64 tag;
  521. u64 lv1_status;
  522. struct completion done;
  523. };
  524. enum ps3_notify_type {
  525. notify_device_ready = 0,
  526. notify_region_probe = 1,
  527. notify_region_update = 2,
  528. };
  529. struct ps3_notify_cmd {
  530. u64 operation_code; /* must be zero */
  531. u64 event_mask; /* OR of 1UL << enum ps3_notify_type */
  532. };
  533. struct ps3_notify_event {
  534. u64 event_type; /* enum ps3_notify_type */
  535. u64 bus_id;
  536. u64 dev_id;
  537. u64 dev_type;
  538. u64 dev_port;
  539. };
  540. static irqreturn_t ps3_notification_interrupt(int irq, void *data)
  541. {
  542. struct ps3_notification_device *dev = data;
  543. int res;
  544. u64 tag, status;
  545. spin_lock(&dev->lock);
  546. res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
  547. &status);
  548. if (tag != dev->tag)
  549. pr_err("%s:%u: tag mismatch, got %lx, expected %lx\n",
  550. __func__, __LINE__, tag, dev->tag);
  551. if (res) {
  552. pr_err("%s:%u: res %d status 0x%lx\n", __func__, __LINE__, res,
  553. status);
  554. } else {
  555. pr_debug("%s:%u: completed, status 0x%lx\n", __func__,
  556. __LINE__, status);
  557. dev->lv1_status = status;
  558. complete(&dev->done);
  559. }
  560. spin_unlock(&dev->lock);
  561. return IRQ_HANDLED;
  562. }
  563. static int ps3_notification_read_write(struct ps3_notification_device *dev,
  564. u64 lpar, int write)
  565. {
  566. const char *op = write ? "write" : "read";
  567. unsigned long flags;
  568. int res;
  569. init_completion(&dev->done);
  570. spin_lock_irqsave(&dev->lock, flags);
  571. res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
  572. &dev->tag)
  573. : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
  574. &dev->tag);
  575. spin_unlock_irqrestore(&dev->lock, flags);
  576. if (res) {
  577. pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
  578. return -EPERM;
  579. }
  580. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  581. res = wait_event_interruptible(dev->done.wait,
  582. dev->done.done || kthread_should_stop());
  583. if (kthread_should_stop())
  584. res = -EINTR;
  585. if (res) {
  586. pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
  587. return res;
  588. }
  589. if (dev->lv1_status) {
  590. pr_err("%s:%u: %s not completed, status 0x%lx\n", __func__,
  591. __LINE__, op, dev->lv1_status);
  592. return -EIO;
  593. }
  594. pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
  595. return 0;
  596. }
  597. static struct task_struct *probe_task;
  598. /**
  599. * ps3_probe_thread - Background repository probing at system startup.
  600. *
  601. * This implementation only supports background probing on a single bus.
  602. * It uses the hypervisor's storage device notification mechanism to wait until
  603. * a storage device is ready. The device notification mechanism uses a
  604. * pseudo device to asynchronously notify the guest when storage devices become
  605. * ready. The notification device has a block size of 512 bytes.
  606. */
  607. static int ps3_probe_thread(void *data)
  608. {
  609. struct ps3_notification_device dev;
  610. int res;
  611. unsigned int irq;
  612. u64 lpar;
  613. void *buf;
  614. struct ps3_notify_cmd *notify_cmd;
  615. struct ps3_notify_event *notify_event;
  616. pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
  617. buf = kzalloc(512, GFP_KERNEL);
  618. if (!buf)
  619. return -ENOMEM;
  620. lpar = ps3_mm_phys_to_lpar(__pa(buf));
  621. notify_cmd = buf;
  622. notify_event = buf;
  623. /* dummy system bus device */
  624. dev.sbd.bus_id = (u64)data;
  625. dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
  626. dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
  627. res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
  628. if (res) {
  629. pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
  630. __LINE__, ps3_result(res));
  631. goto fail_free;
  632. }
  633. res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
  634. &irq);
  635. if (res) {
  636. pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
  637. __func__, __LINE__, res);
  638. goto fail_close_device;
  639. }
  640. spin_lock_init(&dev.lock);
  641. res = request_irq(irq, ps3_notification_interrupt, IRQF_DISABLED,
  642. "ps3_notification", &dev);
  643. if (res) {
  644. pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
  645. res);
  646. goto fail_sb_event_receive_port_destroy;
  647. }
  648. /* Setup and write the request for device notification. */
  649. notify_cmd->operation_code = 0; /* must be zero */
  650. notify_cmd->event_mask = 1UL << notify_region_probe;
  651. res = ps3_notification_read_write(&dev, lpar, 1);
  652. if (res)
  653. goto fail_free_irq;
  654. /* Loop here processing the requested notification events. */
  655. do {
  656. try_to_freeze();
  657. memset(notify_event, 0, sizeof(*notify_event));
  658. res = ps3_notification_read_write(&dev, lpar, 0);
  659. if (res)
  660. break;
  661. pr_debug("%s:%u: notify event type 0x%lx bus id %lu dev id %lu"
  662. " type %lu port %lu\n", __func__, __LINE__,
  663. notify_event->event_type, notify_event->bus_id,
  664. notify_event->dev_id, notify_event->dev_type,
  665. notify_event->dev_port);
  666. if (notify_event->event_type != notify_region_probe ||
  667. notify_event->bus_id != dev.sbd.bus_id) {
  668. pr_warning("%s:%u: bad notify_event: event %lu, "
  669. "dev_id %lu, dev_type %lu\n",
  670. __func__, __LINE__, notify_event->event_type,
  671. notify_event->dev_id,
  672. notify_event->dev_type);
  673. continue;
  674. }
  675. ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
  676. } while (!kthread_should_stop());
  677. fail_free_irq:
  678. free_irq(irq, &dev);
  679. fail_sb_event_receive_port_destroy:
  680. ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
  681. fail_close_device:
  682. lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
  683. fail_free:
  684. kfree(buf);
  685. probe_task = NULL;
  686. pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
  687. return 0;
  688. }
  689. /**
  690. * ps3_stop_probe_thread - Stops the background probe thread.
  691. *
  692. */
  693. static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
  694. void *data)
  695. {
  696. if (probe_task)
  697. kthread_stop(probe_task);
  698. return 0;
  699. }
  700. static struct notifier_block nb = {
  701. .notifier_call = ps3_stop_probe_thread
  702. };
  703. /**
  704. * ps3_start_probe_thread - Starts the background probe thread.
  705. *
  706. */
  707. static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
  708. {
  709. int result;
  710. struct task_struct *task;
  711. struct ps3_repository_device repo;
  712. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  713. memset(&repo, 0, sizeof(repo));
  714. repo.bus_type = bus_type;
  715. result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
  716. if (result) {
  717. printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
  718. return -ENODEV;
  719. }
  720. result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
  721. if (result) {
  722. printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
  723. result);
  724. return -ENODEV;
  725. }
  726. task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
  727. "ps3-probe-%u", bus_type);
  728. if (IS_ERR(task)) {
  729. result = PTR_ERR(task);
  730. printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
  731. result);
  732. return result;
  733. }
  734. probe_task = task;
  735. register_reboot_notifier(&nb);
  736. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  737. return 0;
  738. }
  739. /**
  740. * ps3_register_devices - Probe the system and register devices found.
  741. *
  742. * A device_initcall() routine.
  743. */
  744. static int __init ps3_register_devices(void)
  745. {
  746. int result;
  747. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  748. return -ENODEV;
  749. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  750. /* ps3_repository_dump_bus_info(); */
  751. result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
  752. ps3_register_vuart_devices();
  753. ps3_register_graphics_devices();
  754. ps3_repository_find_devices(PS3_BUS_TYPE_SB, ps3_setup_static_device);
  755. ps3_register_sound_devices();
  756. ps3_register_lpm_devices();
  757. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  758. return 0;
  759. }
  760. device_initcall(ps3_register_devices);