device-init.c 22 KB

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