vio.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IBM PowerPC Virtual I/O Infrastructure Support.
  4. *
  5. * Copyright (c) 2003,2008 IBM Corp.
  6. * Dave Engebretsen [email protected]
  7. * Santiago Leon [email protected]
  8. * Hollis Blanchard <[email protected]>
  9. * Stephen Rothwell
  10. * Robert Jennings <[email protected]>
  11. */
  12. #include <linux/cpu.h>
  13. #include <linux/types.h>
  14. #include <linux/delay.h>
  15. #include <linux/stat.h>
  16. #include <linux/device.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/console.h>
  20. #include <linux/export.h>
  21. #include <linux/mm.h>
  22. #include <linux/dma-map-ops.h>
  23. #include <linux/kobject.h>
  24. #include <linux/kexec.h>
  25. #include <linux/of_irq.h>
  26. #include <asm/iommu.h>
  27. #include <asm/dma.h>
  28. #include <asm/vio.h>
  29. #include <asm/prom.h>
  30. #include <asm/firmware.h>
  31. #include <asm/tce.h>
  32. #include <asm/page.h>
  33. #include <asm/hvcall.h>
  34. #include <asm/machdep.h>
  35. static struct vio_dev vio_bus_device = { /* fake "parent" device */
  36. .name = "vio",
  37. .type = "",
  38. .dev.init_name = "vio",
  39. .dev.bus = &vio_bus_type,
  40. };
  41. #ifdef CONFIG_PPC_SMLPAR
  42. /**
  43. * vio_cmo_pool - A pool of IO memory for CMO use
  44. *
  45. * @size: The size of the pool in bytes
  46. * @free: The amount of free memory in the pool
  47. */
  48. struct vio_cmo_pool {
  49. size_t size;
  50. size_t free;
  51. };
  52. /* How many ms to delay queued balance work */
  53. #define VIO_CMO_BALANCE_DELAY 100
  54. /* Portion out IO memory to CMO devices by this chunk size */
  55. #define VIO_CMO_BALANCE_CHUNK 131072
  56. /**
  57. * vio_cmo_dev_entry - A device that is CMO-enabled and requires entitlement
  58. *
  59. * @vio_dev: struct vio_dev pointer
  60. * @list: pointer to other devices on bus that are being tracked
  61. */
  62. struct vio_cmo_dev_entry {
  63. struct vio_dev *viodev;
  64. struct list_head list;
  65. };
  66. /**
  67. * vio_cmo - VIO bus accounting structure for CMO entitlement
  68. *
  69. * @lock: spinlock for entire structure
  70. * @balance_q: work queue for balancing system entitlement
  71. * @device_list: list of CMO-enabled devices requiring entitlement
  72. * @entitled: total system entitlement in bytes
  73. * @reserve: pool of memory from which devices reserve entitlement, incl. spare
  74. * @excess: pool of excess entitlement not needed for device reserves or spare
  75. * @spare: IO memory for device hotplug functionality
  76. * @min: minimum necessary for system operation
  77. * @desired: desired memory for system operation
  78. * @curr: bytes currently allocated
  79. * @high: high water mark for IO data usage
  80. */
  81. static struct vio_cmo {
  82. spinlock_t lock;
  83. struct delayed_work balance_q;
  84. struct list_head device_list;
  85. size_t entitled;
  86. struct vio_cmo_pool reserve;
  87. struct vio_cmo_pool excess;
  88. size_t spare;
  89. size_t min;
  90. size_t desired;
  91. size_t curr;
  92. size_t high;
  93. } vio_cmo;
  94. /**
  95. * vio_cmo_OF_devices - Count the number of OF devices that have DMA windows
  96. */
  97. static int vio_cmo_num_OF_devs(void)
  98. {
  99. struct device_node *node_vroot;
  100. int count = 0;
  101. /*
  102. * Count the number of vdevice entries with an
  103. * ibm,my-dma-window OF property
  104. */
  105. node_vroot = of_find_node_by_name(NULL, "vdevice");
  106. if (node_vroot) {
  107. struct device_node *of_node;
  108. struct property *prop;
  109. for_each_child_of_node(node_vroot, of_node) {
  110. prop = of_find_property(of_node, "ibm,my-dma-window",
  111. NULL);
  112. if (prop)
  113. count++;
  114. }
  115. }
  116. of_node_put(node_vroot);
  117. return count;
  118. }
  119. /**
  120. * vio_cmo_alloc - allocate IO memory for CMO-enable devices
  121. *
  122. * @viodev: VIO device requesting IO memory
  123. * @size: size of allocation requested
  124. *
  125. * Allocations come from memory reserved for the devices and any excess
  126. * IO memory available to all devices. The spare pool used to service
  127. * hotplug must be equal to %VIO_CMO_MIN_ENT for the excess pool to be
  128. * made available.
  129. *
  130. * Return codes:
  131. * 0 for successful allocation and -ENOMEM for a failure
  132. */
  133. static inline int vio_cmo_alloc(struct vio_dev *viodev, size_t size)
  134. {
  135. unsigned long flags;
  136. size_t reserve_free = 0;
  137. size_t excess_free = 0;
  138. int ret = -ENOMEM;
  139. spin_lock_irqsave(&vio_cmo.lock, flags);
  140. /* Determine the amount of free entitlement available in reserve */
  141. if (viodev->cmo.entitled > viodev->cmo.allocated)
  142. reserve_free = viodev->cmo.entitled - viodev->cmo.allocated;
  143. /* If spare is not fulfilled, the excess pool can not be used. */
  144. if (vio_cmo.spare >= VIO_CMO_MIN_ENT)
  145. excess_free = vio_cmo.excess.free;
  146. /* The request can be satisfied */
  147. if ((reserve_free + excess_free) >= size) {
  148. vio_cmo.curr += size;
  149. if (vio_cmo.curr > vio_cmo.high)
  150. vio_cmo.high = vio_cmo.curr;
  151. viodev->cmo.allocated += size;
  152. size -= min(reserve_free, size);
  153. vio_cmo.excess.free -= size;
  154. ret = 0;
  155. }
  156. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  157. return ret;
  158. }
  159. /**
  160. * vio_cmo_dealloc - deallocate IO memory from CMO-enable devices
  161. * @viodev: VIO device freeing IO memory
  162. * @size: size of deallocation
  163. *
  164. * IO memory is freed by the device back to the correct memory pools.
  165. * The spare pool is replenished first from either memory pool, then
  166. * the reserve pool is used to reduce device entitlement, the excess
  167. * pool is used to increase the reserve pool toward the desired entitlement
  168. * target, and then the remaining memory is returned to the pools.
  169. *
  170. */
  171. static inline void vio_cmo_dealloc(struct vio_dev *viodev, size_t size)
  172. {
  173. unsigned long flags;
  174. size_t spare_needed = 0;
  175. size_t excess_freed = 0;
  176. size_t reserve_freed = size;
  177. size_t tmp;
  178. int balance = 0;
  179. spin_lock_irqsave(&vio_cmo.lock, flags);
  180. vio_cmo.curr -= size;
  181. /* Amount of memory freed from the excess pool */
  182. if (viodev->cmo.allocated > viodev->cmo.entitled) {
  183. excess_freed = min(reserve_freed, (viodev->cmo.allocated -
  184. viodev->cmo.entitled));
  185. reserve_freed -= excess_freed;
  186. }
  187. /* Remove allocation from device */
  188. viodev->cmo.allocated -= (reserve_freed + excess_freed);
  189. /* Spare is a subset of the reserve pool, replenish it first. */
  190. spare_needed = VIO_CMO_MIN_ENT - vio_cmo.spare;
  191. /*
  192. * Replenish the spare in the reserve pool from the excess pool.
  193. * This moves entitlement into the reserve pool.
  194. */
  195. if (spare_needed && excess_freed) {
  196. tmp = min(excess_freed, spare_needed);
  197. vio_cmo.excess.size -= tmp;
  198. vio_cmo.reserve.size += tmp;
  199. vio_cmo.spare += tmp;
  200. excess_freed -= tmp;
  201. spare_needed -= tmp;
  202. balance = 1;
  203. }
  204. /*
  205. * Replenish the spare in the reserve pool from the reserve pool.
  206. * This removes entitlement from the device down to VIO_CMO_MIN_ENT,
  207. * if needed, and gives it to the spare pool. The amount of used
  208. * memory in this pool does not change.
  209. */
  210. if (spare_needed && reserve_freed) {
  211. tmp = min3(spare_needed, reserve_freed, (viodev->cmo.entitled - VIO_CMO_MIN_ENT));
  212. vio_cmo.spare += tmp;
  213. viodev->cmo.entitled -= tmp;
  214. reserve_freed -= tmp;
  215. spare_needed -= tmp;
  216. balance = 1;
  217. }
  218. /*
  219. * Increase the reserve pool until the desired allocation is met.
  220. * Move an allocation freed from the excess pool into the reserve
  221. * pool and schedule a balance operation.
  222. */
  223. if (excess_freed && (vio_cmo.desired > vio_cmo.reserve.size)) {
  224. tmp = min(excess_freed, (vio_cmo.desired - vio_cmo.reserve.size));
  225. vio_cmo.excess.size -= tmp;
  226. vio_cmo.reserve.size += tmp;
  227. excess_freed -= tmp;
  228. balance = 1;
  229. }
  230. /* Return memory from the excess pool to that pool */
  231. if (excess_freed)
  232. vio_cmo.excess.free += excess_freed;
  233. if (balance)
  234. schedule_delayed_work(&vio_cmo.balance_q, VIO_CMO_BALANCE_DELAY);
  235. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  236. }
  237. /**
  238. * vio_cmo_entitlement_update - Manage system entitlement changes
  239. *
  240. * @new_entitlement: new system entitlement to attempt to accommodate
  241. *
  242. * Increases in entitlement will be used to fulfill the spare entitlement
  243. * and the rest is given to the excess pool. Decreases, if they are
  244. * possible, come from the excess pool and from unused device entitlement
  245. *
  246. * Returns: 0 on success, -ENOMEM when change can not be made
  247. */
  248. int vio_cmo_entitlement_update(size_t new_entitlement)
  249. {
  250. struct vio_dev *viodev;
  251. struct vio_cmo_dev_entry *dev_ent;
  252. unsigned long flags;
  253. size_t avail, delta, tmp;
  254. spin_lock_irqsave(&vio_cmo.lock, flags);
  255. /* Entitlement increases */
  256. if (new_entitlement > vio_cmo.entitled) {
  257. delta = new_entitlement - vio_cmo.entitled;
  258. /* Fulfill spare allocation */
  259. if (vio_cmo.spare < VIO_CMO_MIN_ENT) {
  260. tmp = min(delta, (VIO_CMO_MIN_ENT - vio_cmo.spare));
  261. vio_cmo.spare += tmp;
  262. vio_cmo.reserve.size += tmp;
  263. delta -= tmp;
  264. }
  265. /* Remaining new allocation goes to the excess pool */
  266. vio_cmo.entitled += delta;
  267. vio_cmo.excess.size += delta;
  268. vio_cmo.excess.free += delta;
  269. goto out;
  270. }
  271. /* Entitlement decreases */
  272. delta = vio_cmo.entitled - new_entitlement;
  273. avail = vio_cmo.excess.free;
  274. /*
  275. * Need to check how much unused entitlement each device can
  276. * sacrifice to fulfill entitlement change.
  277. */
  278. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  279. if (avail >= delta)
  280. break;
  281. viodev = dev_ent->viodev;
  282. if ((viodev->cmo.entitled > viodev->cmo.allocated) &&
  283. (viodev->cmo.entitled > VIO_CMO_MIN_ENT))
  284. avail += viodev->cmo.entitled -
  285. max_t(size_t, viodev->cmo.allocated,
  286. VIO_CMO_MIN_ENT);
  287. }
  288. if (delta <= avail) {
  289. vio_cmo.entitled -= delta;
  290. /* Take entitlement from the excess pool first */
  291. tmp = min(vio_cmo.excess.free, delta);
  292. vio_cmo.excess.size -= tmp;
  293. vio_cmo.excess.free -= tmp;
  294. delta -= tmp;
  295. /*
  296. * Remove all but VIO_CMO_MIN_ENT bytes from devices
  297. * until entitlement change is served
  298. */
  299. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  300. if (!delta)
  301. break;
  302. viodev = dev_ent->viodev;
  303. tmp = 0;
  304. if ((viodev->cmo.entitled > viodev->cmo.allocated) &&
  305. (viodev->cmo.entitled > VIO_CMO_MIN_ENT))
  306. tmp = viodev->cmo.entitled -
  307. max_t(size_t, viodev->cmo.allocated,
  308. VIO_CMO_MIN_ENT);
  309. viodev->cmo.entitled -= min(tmp, delta);
  310. delta -= min(tmp, delta);
  311. }
  312. } else {
  313. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  314. return -ENOMEM;
  315. }
  316. out:
  317. schedule_delayed_work(&vio_cmo.balance_q, 0);
  318. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  319. return 0;
  320. }
  321. /**
  322. * vio_cmo_balance - Balance entitlement among devices
  323. *
  324. * @work: work queue structure for this operation
  325. *
  326. * Any system entitlement above the minimum needed for devices, or
  327. * already allocated to devices, can be distributed to the devices.
  328. * The list of devices is iterated through to recalculate the desired
  329. * entitlement level and to determine how much entitlement above the
  330. * minimum entitlement is allocated to devices.
  331. *
  332. * Small chunks of the available entitlement are given to devices until
  333. * their requirements are fulfilled or there is no entitlement left to give.
  334. * Upon completion sizes of the reserve and excess pools are calculated.
  335. *
  336. * The system minimum entitlement level is also recalculated here.
  337. * Entitlement will be reserved for devices even after vio_bus_remove to
  338. * accommodate reloading the driver. The OF tree is walked to count the
  339. * number of devices present and this will remove entitlement for devices
  340. * that have actually left the system after having vio_bus_remove called.
  341. */
  342. static void vio_cmo_balance(struct work_struct *work)
  343. {
  344. struct vio_cmo *cmo;
  345. struct vio_dev *viodev;
  346. struct vio_cmo_dev_entry *dev_ent;
  347. unsigned long flags;
  348. size_t avail = 0, level, chunk, need;
  349. int devcount = 0, fulfilled;
  350. cmo = container_of(work, struct vio_cmo, balance_q.work);
  351. spin_lock_irqsave(&vio_cmo.lock, flags);
  352. /* Calculate minimum entitlement and fulfill spare */
  353. cmo->min = vio_cmo_num_OF_devs() * VIO_CMO_MIN_ENT;
  354. BUG_ON(cmo->min > cmo->entitled);
  355. cmo->spare = min_t(size_t, VIO_CMO_MIN_ENT, (cmo->entitled - cmo->min));
  356. cmo->min += cmo->spare;
  357. cmo->desired = cmo->min;
  358. /*
  359. * Determine how much entitlement is available and reset device
  360. * entitlements
  361. */
  362. avail = cmo->entitled - cmo->spare;
  363. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  364. viodev = dev_ent->viodev;
  365. devcount++;
  366. viodev->cmo.entitled = VIO_CMO_MIN_ENT;
  367. cmo->desired += (viodev->cmo.desired - VIO_CMO_MIN_ENT);
  368. avail -= max_t(size_t, viodev->cmo.allocated, VIO_CMO_MIN_ENT);
  369. }
  370. /*
  371. * Having provided each device with the minimum entitlement, loop
  372. * over the devices portioning out the remaining entitlement
  373. * until there is nothing left.
  374. */
  375. level = VIO_CMO_MIN_ENT;
  376. while (avail) {
  377. fulfilled = 0;
  378. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  379. viodev = dev_ent->viodev;
  380. if (viodev->cmo.desired <= level) {
  381. fulfilled++;
  382. continue;
  383. }
  384. /*
  385. * Give the device up to VIO_CMO_BALANCE_CHUNK
  386. * bytes of entitlement, but do not exceed the
  387. * desired level of entitlement for the device.
  388. */
  389. chunk = min_t(size_t, avail, VIO_CMO_BALANCE_CHUNK);
  390. chunk = min(chunk, (viodev->cmo.desired -
  391. viodev->cmo.entitled));
  392. viodev->cmo.entitled += chunk;
  393. /*
  394. * If the memory for this entitlement increase was
  395. * already allocated to the device it does not come
  396. * from the available pool being portioned out.
  397. */
  398. need = max(viodev->cmo.allocated, viodev->cmo.entitled)-
  399. max(viodev->cmo.allocated, level);
  400. avail -= need;
  401. }
  402. if (fulfilled == devcount)
  403. break;
  404. level += VIO_CMO_BALANCE_CHUNK;
  405. }
  406. /* Calculate new reserve and excess pool sizes */
  407. cmo->reserve.size = cmo->min;
  408. cmo->excess.free = 0;
  409. cmo->excess.size = 0;
  410. need = 0;
  411. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  412. viodev = dev_ent->viodev;
  413. /* Calculated reserve size above the minimum entitlement */
  414. if (viodev->cmo.entitled)
  415. cmo->reserve.size += (viodev->cmo.entitled -
  416. VIO_CMO_MIN_ENT);
  417. /* Calculated used excess entitlement */
  418. if (viodev->cmo.allocated > viodev->cmo.entitled)
  419. need += viodev->cmo.allocated - viodev->cmo.entitled;
  420. }
  421. cmo->excess.size = cmo->entitled - cmo->reserve.size;
  422. cmo->excess.free = cmo->excess.size - need;
  423. cancel_delayed_work(to_delayed_work(work));
  424. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  425. }
  426. static void *vio_dma_iommu_alloc_coherent(struct device *dev, size_t size,
  427. dma_addr_t *dma_handle, gfp_t flag,
  428. unsigned long attrs)
  429. {
  430. struct vio_dev *viodev = to_vio_dev(dev);
  431. void *ret;
  432. if (vio_cmo_alloc(viodev, roundup(size, PAGE_SIZE))) {
  433. atomic_inc(&viodev->cmo.allocs_failed);
  434. return NULL;
  435. }
  436. ret = iommu_alloc_coherent(dev, get_iommu_table_base(dev), size,
  437. dma_handle, dev->coherent_dma_mask, flag,
  438. dev_to_node(dev));
  439. if (unlikely(ret == NULL)) {
  440. vio_cmo_dealloc(viodev, roundup(size, PAGE_SIZE));
  441. atomic_inc(&viodev->cmo.allocs_failed);
  442. }
  443. return ret;
  444. }
  445. static void vio_dma_iommu_free_coherent(struct device *dev, size_t size,
  446. void *vaddr, dma_addr_t dma_handle,
  447. unsigned long attrs)
  448. {
  449. struct vio_dev *viodev = to_vio_dev(dev);
  450. iommu_free_coherent(get_iommu_table_base(dev), size, vaddr, dma_handle);
  451. vio_cmo_dealloc(viodev, roundup(size, PAGE_SIZE));
  452. }
  453. static dma_addr_t vio_dma_iommu_map_page(struct device *dev, struct page *page,
  454. unsigned long offset, size_t size,
  455. enum dma_data_direction direction,
  456. unsigned long attrs)
  457. {
  458. struct vio_dev *viodev = to_vio_dev(dev);
  459. struct iommu_table *tbl = get_iommu_table_base(dev);
  460. dma_addr_t ret = DMA_MAPPING_ERROR;
  461. if (vio_cmo_alloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl))))
  462. goto out_fail;
  463. ret = iommu_map_page(dev, tbl, page, offset, size, dma_get_mask(dev),
  464. direction, attrs);
  465. if (unlikely(ret == DMA_MAPPING_ERROR))
  466. goto out_deallocate;
  467. return ret;
  468. out_deallocate:
  469. vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)));
  470. out_fail:
  471. atomic_inc(&viodev->cmo.allocs_failed);
  472. return DMA_MAPPING_ERROR;
  473. }
  474. static void vio_dma_iommu_unmap_page(struct device *dev, dma_addr_t dma_handle,
  475. size_t size,
  476. enum dma_data_direction direction,
  477. unsigned long attrs)
  478. {
  479. struct vio_dev *viodev = to_vio_dev(dev);
  480. struct iommu_table *tbl = get_iommu_table_base(dev);
  481. iommu_unmap_page(tbl, dma_handle, size, direction, attrs);
  482. vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)));
  483. }
  484. static int vio_dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
  485. int nelems, enum dma_data_direction direction,
  486. unsigned long attrs)
  487. {
  488. struct vio_dev *viodev = to_vio_dev(dev);
  489. struct iommu_table *tbl = get_iommu_table_base(dev);
  490. struct scatterlist *sgl;
  491. int ret, count;
  492. size_t alloc_size = 0;
  493. for_each_sg(sglist, sgl, nelems, count)
  494. alloc_size += roundup(sgl->length, IOMMU_PAGE_SIZE(tbl));
  495. ret = vio_cmo_alloc(viodev, alloc_size);
  496. if (ret)
  497. goto out_fail;
  498. ret = ppc_iommu_map_sg(dev, tbl, sglist, nelems, dma_get_mask(dev),
  499. direction, attrs);
  500. if (unlikely(!ret))
  501. goto out_deallocate;
  502. for_each_sg(sglist, sgl, ret, count)
  503. alloc_size -= roundup(sgl->dma_length, IOMMU_PAGE_SIZE(tbl));
  504. if (alloc_size)
  505. vio_cmo_dealloc(viodev, alloc_size);
  506. return ret;
  507. out_deallocate:
  508. vio_cmo_dealloc(viodev, alloc_size);
  509. out_fail:
  510. atomic_inc(&viodev->cmo.allocs_failed);
  511. return ret;
  512. }
  513. static void vio_dma_iommu_unmap_sg(struct device *dev,
  514. struct scatterlist *sglist, int nelems,
  515. enum dma_data_direction direction,
  516. unsigned long attrs)
  517. {
  518. struct vio_dev *viodev = to_vio_dev(dev);
  519. struct iommu_table *tbl = get_iommu_table_base(dev);
  520. struct scatterlist *sgl;
  521. size_t alloc_size = 0;
  522. int count;
  523. for_each_sg(sglist, sgl, nelems, count)
  524. alloc_size += roundup(sgl->dma_length, IOMMU_PAGE_SIZE(tbl));
  525. ppc_iommu_unmap_sg(tbl, sglist, nelems, direction, attrs);
  526. vio_cmo_dealloc(viodev, alloc_size);
  527. }
  528. static const struct dma_map_ops vio_dma_mapping_ops = {
  529. .alloc = vio_dma_iommu_alloc_coherent,
  530. .free = vio_dma_iommu_free_coherent,
  531. .map_sg = vio_dma_iommu_map_sg,
  532. .unmap_sg = vio_dma_iommu_unmap_sg,
  533. .map_page = vio_dma_iommu_map_page,
  534. .unmap_page = vio_dma_iommu_unmap_page,
  535. .dma_supported = dma_iommu_dma_supported,
  536. .get_required_mask = dma_iommu_get_required_mask,
  537. .mmap = dma_common_mmap,
  538. .get_sgtable = dma_common_get_sgtable,
  539. .alloc_pages = dma_common_alloc_pages,
  540. .free_pages = dma_common_free_pages,
  541. };
  542. /**
  543. * vio_cmo_set_dev_desired - Set desired entitlement for a device
  544. *
  545. * @viodev: struct vio_dev for device to alter
  546. * @desired: new desired entitlement level in bytes
  547. *
  548. * For use by devices to request a change to their entitlement at runtime or
  549. * through sysfs. The desired entitlement level is changed and a balancing
  550. * of system resources is scheduled to run in the future.
  551. */
  552. void vio_cmo_set_dev_desired(struct vio_dev *viodev, size_t desired)
  553. {
  554. unsigned long flags;
  555. struct vio_cmo_dev_entry *dev_ent;
  556. int found = 0;
  557. if (!firmware_has_feature(FW_FEATURE_CMO))
  558. return;
  559. spin_lock_irqsave(&vio_cmo.lock, flags);
  560. if (desired < VIO_CMO_MIN_ENT)
  561. desired = VIO_CMO_MIN_ENT;
  562. /*
  563. * Changes will not be made for devices not in the device list.
  564. * If it is not in the device list, then no driver is loaded
  565. * for the device and it can not receive entitlement.
  566. */
  567. list_for_each_entry(dev_ent, &vio_cmo.device_list, list)
  568. if (viodev == dev_ent->viodev) {
  569. found = 1;
  570. break;
  571. }
  572. if (!found) {
  573. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  574. return;
  575. }
  576. /* Increase/decrease in desired device entitlement */
  577. if (desired >= viodev->cmo.desired) {
  578. /* Just bump the bus and device values prior to a balance*/
  579. vio_cmo.desired += desired - viodev->cmo.desired;
  580. viodev->cmo.desired = desired;
  581. } else {
  582. /* Decrease bus and device values for desired entitlement */
  583. vio_cmo.desired -= viodev->cmo.desired - desired;
  584. viodev->cmo.desired = desired;
  585. /*
  586. * If less entitlement is desired than current entitlement, move
  587. * any reserve memory in the change region to the excess pool.
  588. */
  589. if (viodev->cmo.entitled > desired) {
  590. vio_cmo.reserve.size -= viodev->cmo.entitled - desired;
  591. vio_cmo.excess.size += viodev->cmo.entitled - desired;
  592. /*
  593. * If entitlement moving from the reserve pool to the
  594. * excess pool is currently unused, add to the excess
  595. * free counter.
  596. */
  597. if (viodev->cmo.allocated < viodev->cmo.entitled)
  598. vio_cmo.excess.free += viodev->cmo.entitled -
  599. max(viodev->cmo.allocated, desired);
  600. viodev->cmo.entitled = desired;
  601. }
  602. }
  603. schedule_delayed_work(&vio_cmo.balance_q, 0);
  604. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  605. }
  606. /**
  607. * vio_cmo_bus_probe - Handle CMO specific bus probe activities
  608. *
  609. * @viodev - Pointer to struct vio_dev for device
  610. *
  611. * Determine the devices IO memory entitlement needs, attempting
  612. * to satisfy the system minimum entitlement at first and scheduling
  613. * a balance operation to take care of the rest at a later time.
  614. *
  615. * Returns: 0 on success, -EINVAL when device doesn't support CMO, and
  616. * -ENOMEM when entitlement is not available for device or
  617. * device entry.
  618. *
  619. */
  620. static int vio_cmo_bus_probe(struct vio_dev *viodev)
  621. {
  622. struct vio_cmo_dev_entry *dev_ent;
  623. struct device *dev = &viodev->dev;
  624. struct iommu_table *tbl;
  625. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  626. unsigned long flags;
  627. size_t size;
  628. bool dma_capable = false;
  629. tbl = get_iommu_table_base(dev);
  630. /* A device requires entitlement if it has a DMA window property */
  631. switch (viodev->family) {
  632. case VDEVICE:
  633. if (of_get_property(viodev->dev.of_node,
  634. "ibm,my-dma-window", NULL))
  635. dma_capable = true;
  636. break;
  637. case PFO:
  638. dma_capable = false;
  639. break;
  640. default:
  641. dev_warn(dev, "unknown device family: %d\n", viodev->family);
  642. BUG();
  643. break;
  644. }
  645. /* Configure entitlement for the device. */
  646. if (dma_capable) {
  647. /* Check that the driver is CMO enabled and get desired DMA */
  648. if (!viodrv->get_desired_dma) {
  649. dev_err(dev, "%s: device driver does not support CMO\n",
  650. __func__);
  651. return -EINVAL;
  652. }
  653. viodev->cmo.desired =
  654. IOMMU_PAGE_ALIGN(viodrv->get_desired_dma(viodev), tbl);
  655. if (viodev->cmo.desired < VIO_CMO_MIN_ENT)
  656. viodev->cmo.desired = VIO_CMO_MIN_ENT;
  657. size = VIO_CMO_MIN_ENT;
  658. dev_ent = kmalloc(sizeof(struct vio_cmo_dev_entry),
  659. GFP_KERNEL);
  660. if (!dev_ent)
  661. return -ENOMEM;
  662. dev_ent->viodev = viodev;
  663. spin_lock_irqsave(&vio_cmo.lock, flags);
  664. list_add(&dev_ent->list, &vio_cmo.device_list);
  665. } else {
  666. viodev->cmo.desired = 0;
  667. size = 0;
  668. spin_lock_irqsave(&vio_cmo.lock, flags);
  669. }
  670. /*
  671. * If the needs for vio_cmo.min have not changed since they
  672. * were last set, the number of devices in the OF tree has
  673. * been constant and the IO memory for this is already in
  674. * the reserve pool.
  675. */
  676. if (vio_cmo.min == ((vio_cmo_num_OF_devs() + 1) *
  677. VIO_CMO_MIN_ENT)) {
  678. /* Updated desired entitlement if device requires it */
  679. if (size)
  680. vio_cmo.desired += (viodev->cmo.desired -
  681. VIO_CMO_MIN_ENT);
  682. } else {
  683. size_t tmp;
  684. tmp = vio_cmo.spare + vio_cmo.excess.free;
  685. if (tmp < size) {
  686. dev_err(dev, "%s: insufficient free "
  687. "entitlement to add device. "
  688. "Need %lu, have %lu\n", __func__,
  689. size, (vio_cmo.spare + tmp));
  690. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  691. return -ENOMEM;
  692. }
  693. /* Use excess pool first to fulfill request */
  694. tmp = min(size, vio_cmo.excess.free);
  695. vio_cmo.excess.free -= tmp;
  696. vio_cmo.excess.size -= tmp;
  697. vio_cmo.reserve.size += tmp;
  698. /* Use spare if excess pool was insufficient */
  699. vio_cmo.spare -= size - tmp;
  700. /* Update bus accounting */
  701. vio_cmo.min += size;
  702. vio_cmo.desired += viodev->cmo.desired;
  703. }
  704. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  705. return 0;
  706. }
  707. /**
  708. * vio_cmo_bus_remove - Handle CMO specific bus removal activities
  709. *
  710. * @viodev - Pointer to struct vio_dev for device
  711. *
  712. * Remove the device from the cmo device list. The minimum entitlement
  713. * will be reserved for the device as long as it is in the system. The
  714. * rest of the entitlement the device had been allocated will be returned
  715. * to the system.
  716. */
  717. static void vio_cmo_bus_remove(struct vio_dev *viodev)
  718. {
  719. struct vio_cmo_dev_entry *dev_ent;
  720. unsigned long flags;
  721. size_t tmp;
  722. spin_lock_irqsave(&vio_cmo.lock, flags);
  723. if (viodev->cmo.allocated) {
  724. dev_err(&viodev->dev, "%s: device had %lu bytes of IO "
  725. "allocated after remove operation.\n",
  726. __func__, viodev->cmo.allocated);
  727. BUG();
  728. }
  729. /*
  730. * Remove the device from the device list being maintained for
  731. * CMO enabled devices.
  732. */
  733. list_for_each_entry(dev_ent, &vio_cmo.device_list, list)
  734. if (viodev == dev_ent->viodev) {
  735. list_del(&dev_ent->list);
  736. kfree(dev_ent);
  737. break;
  738. }
  739. /*
  740. * Devices may not require any entitlement and they do not need
  741. * to be processed. Otherwise, return the device's entitlement
  742. * back to the pools.
  743. */
  744. if (viodev->cmo.entitled) {
  745. /*
  746. * This device has not yet left the OF tree, it's
  747. * minimum entitlement remains in vio_cmo.min and
  748. * vio_cmo.desired
  749. */
  750. vio_cmo.desired -= (viodev->cmo.desired - VIO_CMO_MIN_ENT);
  751. /*
  752. * Save min allocation for device in reserve as long
  753. * as it exists in OF tree as determined by later
  754. * balance operation
  755. */
  756. viodev->cmo.entitled -= VIO_CMO_MIN_ENT;
  757. /* Replenish spare from freed reserve pool */
  758. if (viodev->cmo.entitled && (vio_cmo.spare < VIO_CMO_MIN_ENT)) {
  759. tmp = min(viodev->cmo.entitled, (VIO_CMO_MIN_ENT -
  760. vio_cmo.spare));
  761. vio_cmo.spare += tmp;
  762. viodev->cmo.entitled -= tmp;
  763. }
  764. /* Remaining reserve goes to excess pool */
  765. vio_cmo.excess.size += viodev->cmo.entitled;
  766. vio_cmo.excess.free += viodev->cmo.entitled;
  767. vio_cmo.reserve.size -= viodev->cmo.entitled;
  768. /*
  769. * Until the device is removed it will keep a
  770. * minimum entitlement; this will guarantee that
  771. * a module unload/load will result in a success.
  772. */
  773. viodev->cmo.entitled = VIO_CMO_MIN_ENT;
  774. viodev->cmo.desired = VIO_CMO_MIN_ENT;
  775. atomic_set(&viodev->cmo.allocs_failed, 0);
  776. }
  777. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  778. }
  779. static void vio_cmo_set_dma_ops(struct vio_dev *viodev)
  780. {
  781. set_dma_ops(&viodev->dev, &vio_dma_mapping_ops);
  782. }
  783. /**
  784. * vio_cmo_bus_init - CMO entitlement initialization at bus init time
  785. *
  786. * Set up the reserve and excess entitlement pools based on available
  787. * system entitlement and the number of devices in the OF tree that
  788. * require entitlement in the reserve pool.
  789. */
  790. static void vio_cmo_bus_init(void)
  791. {
  792. struct hvcall_mpp_data mpp_data;
  793. int err;
  794. memset(&vio_cmo, 0, sizeof(struct vio_cmo));
  795. spin_lock_init(&vio_cmo.lock);
  796. INIT_LIST_HEAD(&vio_cmo.device_list);
  797. INIT_DELAYED_WORK(&vio_cmo.balance_q, vio_cmo_balance);
  798. /* Get current system entitlement */
  799. err = h_get_mpp(&mpp_data);
  800. /*
  801. * On failure, continue with entitlement set to 0, will panic()
  802. * later when spare is reserved.
  803. */
  804. if (err != H_SUCCESS) {
  805. printk(KERN_ERR "%s: unable to determine system IO "\
  806. "entitlement. (%d)\n", __func__, err);
  807. vio_cmo.entitled = 0;
  808. } else {
  809. vio_cmo.entitled = mpp_data.entitled_mem;
  810. }
  811. /* Set reservation and check against entitlement */
  812. vio_cmo.spare = VIO_CMO_MIN_ENT;
  813. vio_cmo.reserve.size = vio_cmo.spare;
  814. vio_cmo.reserve.size += (vio_cmo_num_OF_devs() *
  815. VIO_CMO_MIN_ENT);
  816. if (vio_cmo.reserve.size > vio_cmo.entitled) {
  817. printk(KERN_ERR "%s: insufficient system entitlement\n",
  818. __func__);
  819. panic("%s: Insufficient system entitlement", __func__);
  820. }
  821. /* Set the remaining accounting variables */
  822. vio_cmo.excess.size = vio_cmo.entitled - vio_cmo.reserve.size;
  823. vio_cmo.excess.free = vio_cmo.excess.size;
  824. vio_cmo.min = vio_cmo.reserve.size;
  825. vio_cmo.desired = vio_cmo.reserve.size;
  826. }
  827. /* sysfs device functions and data structures for CMO */
  828. #define viodev_cmo_rd_attr(name) \
  829. static ssize_t cmo_##name##_show(struct device *dev, \
  830. struct device_attribute *attr, \
  831. char *buf) \
  832. { \
  833. return sprintf(buf, "%lu\n", to_vio_dev(dev)->cmo.name); \
  834. }
  835. static ssize_t cmo_allocs_failed_show(struct device *dev,
  836. struct device_attribute *attr, char *buf)
  837. {
  838. struct vio_dev *viodev = to_vio_dev(dev);
  839. return sprintf(buf, "%d\n", atomic_read(&viodev->cmo.allocs_failed));
  840. }
  841. static ssize_t cmo_allocs_failed_store(struct device *dev,
  842. struct device_attribute *attr, const char *buf, size_t count)
  843. {
  844. struct vio_dev *viodev = to_vio_dev(dev);
  845. atomic_set(&viodev->cmo.allocs_failed, 0);
  846. return count;
  847. }
  848. static ssize_t cmo_desired_store(struct device *dev,
  849. struct device_attribute *attr, const char *buf, size_t count)
  850. {
  851. struct vio_dev *viodev = to_vio_dev(dev);
  852. size_t new_desired;
  853. int ret;
  854. ret = kstrtoul(buf, 10, &new_desired);
  855. if (ret)
  856. return ret;
  857. vio_cmo_set_dev_desired(viodev, new_desired);
  858. return count;
  859. }
  860. viodev_cmo_rd_attr(desired);
  861. viodev_cmo_rd_attr(entitled);
  862. viodev_cmo_rd_attr(allocated);
  863. static ssize_t name_show(struct device *, struct device_attribute *, char *);
  864. static ssize_t devspec_show(struct device *, struct device_attribute *, char *);
  865. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  866. char *buf);
  867. static struct device_attribute dev_attr_name;
  868. static struct device_attribute dev_attr_devspec;
  869. static struct device_attribute dev_attr_modalias;
  870. static DEVICE_ATTR_RO(cmo_entitled);
  871. static DEVICE_ATTR_RO(cmo_allocated);
  872. static DEVICE_ATTR_RW(cmo_desired);
  873. static DEVICE_ATTR_RW(cmo_allocs_failed);
  874. static struct attribute *vio_cmo_dev_attrs[] = {
  875. &dev_attr_name.attr,
  876. &dev_attr_devspec.attr,
  877. &dev_attr_modalias.attr,
  878. &dev_attr_cmo_entitled.attr,
  879. &dev_attr_cmo_allocated.attr,
  880. &dev_attr_cmo_desired.attr,
  881. &dev_attr_cmo_allocs_failed.attr,
  882. NULL,
  883. };
  884. ATTRIBUTE_GROUPS(vio_cmo_dev);
  885. /* sysfs bus functions and data structures for CMO */
  886. #define viobus_cmo_rd_attr(name) \
  887. static ssize_t cmo_bus_##name##_show(struct bus_type *bt, char *buf) \
  888. { \
  889. return sprintf(buf, "%lu\n", vio_cmo.name); \
  890. } \
  891. static struct bus_attribute bus_attr_cmo_bus_##name = \
  892. __ATTR(cmo_##name, S_IRUGO, cmo_bus_##name##_show, NULL)
  893. #define viobus_cmo_pool_rd_attr(name, var) \
  894. static ssize_t \
  895. cmo_##name##_##var##_show(struct bus_type *bt, char *buf) \
  896. { \
  897. return sprintf(buf, "%lu\n", vio_cmo.name.var); \
  898. } \
  899. static BUS_ATTR_RO(cmo_##name##_##var)
  900. viobus_cmo_rd_attr(entitled);
  901. viobus_cmo_rd_attr(spare);
  902. viobus_cmo_rd_attr(min);
  903. viobus_cmo_rd_attr(desired);
  904. viobus_cmo_rd_attr(curr);
  905. viobus_cmo_pool_rd_attr(reserve, size);
  906. viobus_cmo_pool_rd_attr(excess, size);
  907. viobus_cmo_pool_rd_attr(excess, free);
  908. static ssize_t cmo_high_show(struct bus_type *bt, char *buf)
  909. {
  910. return sprintf(buf, "%lu\n", vio_cmo.high);
  911. }
  912. static ssize_t cmo_high_store(struct bus_type *bt, const char *buf,
  913. size_t count)
  914. {
  915. unsigned long flags;
  916. spin_lock_irqsave(&vio_cmo.lock, flags);
  917. vio_cmo.high = vio_cmo.curr;
  918. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  919. return count;
  920. }
  921. static BUS_ATTR_RW(cmo_high);
  922. static struct attribute *vio_bus_attrs[] = {
  923. &bus_attr_cmo_bus_entitled.attr,
  924. &bus_attr_cmo_bus_spare.attr,
  925. &bus_attr_cmo_bus_min.attr,
  926. &bus_attr_cmo_bus_desired.attr,
  927. &bus_attr_cmo_bus_curr.attr,
  928. &bus_attr_cmo_high.attr,
  929. &bus_attr_cmo_reserve_size.attr,
  930. &bus_attr_cmo_excess_size.attr,
  931. &bus_attr_cmo_excess_free.attr,
  932. NULL,
  933. };
  934. ATTRIBUTE_GROUPS(vio_bus);
  935. static void __init vio_cmo_sysfs_init(void)
  936. {
  937. vio_bus_type.dev_groups = vio_cmo_dev_groups;
  938. vio_bus_type.bus_groups = vio_bus_groups;
  939. }
  940. #else /* CONFIG_PPC_SMLPAR */
  941. int vio_cmo_entitlement_update(size_t new_entitlement) { return 0; }
  942. void vio_cmo_set_dev_desired(struct vio_dev *viodev, size_t desired) {}
  943. static int vio_cmo_bus_probe(struct vio_dev *viodev) { return 0; }
  944. static void vio_cmo_bus_remove(struct vio_dev *viodev) {}
  945. static void vio_cmo_set_dma_ops(struct vio_dev *viodev) {}
  946. static void vio_cmo_bus_init(void) {}
  947. static void __init vio_cmo_sysfs_init(void) { }
  948. #endif /* CONFIG_PPC_SMLPAR */
  949. EXPORT_SYMBOL(vio_cmo_entitlement_update);
  950. EXPORT_SYMBOL(vio_cmo_set_dev_desired);
  951. /*
  952. * Platform Facilities Option (PFO) support
  953. */
  954. /**
  955. * vio_h_cop_sync - Perform a synchronous PFO co-processor operation
  956. *
  957. * @vdev - Pointer to a struct vio_dev for device
  958. * @op - Pointer to a struct vio_pfo_op for the operation parameters
  959. *
  960. * Calls the hypervisor to synchronously perform the PFO operation
  961. * described in @op. In the case of a busy response from the hypervisor,
  962. * the operation will be re-submitted indefinitely unless a non-zero timeout
  963. * is specified or an error occurs. The timeout places a limit on when to
  964. * stop re-submitting a operation, the total time can be exceeded if an
  965. * operation is in progress.
  966. *
  967. * If op->hcall_ret is not NULL, this will be set to the return from the
  968. * last h_cop_op call or it will be 0 if an error not involving the h_call
  969. * was encountered.
  970. *
  971. * Returns:
  972. * 0 on success,
  973. * -EINVAL if the h_call fails due to an invalid parameter,
  974. * -E2BIG if the h_call can not be performed synchronously,
  975. * -EBUSY if a timeout is specified and has elapsed,
  976. * -EACCES if the memory area for data/status has been rescinded, or
  977. * -EPERM if a hardware fault has been indicated
  978. */
  979. int vio_h_cop_sync(struct vio_dev *vdev, struct vio_pfo_op *op)
  980. {
  981. struct device *dev = &vdev->dev;
  982. unsigned long deadline = 0;
  983. long hret = 0;
  984. int ret = 0;
  985. if (op->timeout)
  986. deadline = jiffies + msecs_to_jiffies(op->timeout);
  987. while (true) {
  988. hret = plpar_hcall_norets(H_COP, op->flags,
  989. vdev->resource_id,
  990. op->in, op->inlen, op->out,
  991. op->outlen, op->csbcpb);
  992. if (hret == H_SUCCESS ||
  993. (hret != H_NOT_ENOUGH_RESOURCES &&
  994. hret != H_BUSY && hret != H_RESOURCE) ||
  995. (op->timeout && time_after(deadline, jiffies)))
  996. break;
  997. dev_dbg(dev, "%s: hcall ret(%ld), retrying.\n", __func__, hret);
  998. }
  999. switch (hret) {
  1000. case H_SUCCESS:
  1001. ret = 0;
  1002. break;
  1003. case H_OP_MODE:
  1004. case H_TOO_BIG:
  1005. ret = -E2BIG;
  1006. break;
  1007. case H_RESCINDED:
  1008. ret = -EACCES;
  1009. break;
  1010. case H_HARDWARE:
  1011. ret = -EPERM;
  1012. break;
  1013. case H_NOT_ENOUGH_RESOURCES:
  1014. case H_RESOURCE:
  1015. case H_BUSY:
  1016. ret = -EBUSY;
  1017. break;
  1018. default:
  1019. ret = -EINVAL;
  1020. break;
  1021. }
  1022. if (ret)
  1023. dev_dbg(dev, "%s: Sync h_cop_op failure (ret:%d) (hret:%ld)\n",
  1024. __func__, ret, hret);
  1025. op->hcall_err = hret;
  1026. return ret;
  1027. }
  1028. EXPORT_SYMBOL(vio_h_cop_sync);
  1029. static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
  1030. {
  1031. const __be32 *dma_window;
  1032. struct iommu_table *tbl;
  1033. unsigned long offset, size;
  1034. dma_window = of_get_property(dev->dev.of_node,
  1035. "ibm,my-dma-window", NULL);
  1036. if (!dma_window)
  1037. return NULL;
  1038. tbl = kzalloc(sizeof(*tbl), GFP_KERNEL);
  1039. if (tbl == NULL)
  1040. return NULL;
  1041. kref_init(&tbl->it_kref);
  1042. of_parse_dma_window(dev->dev.of_node, dma_window,
  1043. &tbl->it_index, &offset, &size);
  1044. /* TCE table size - measured in tce entries */
  1045. tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
  1046. tbl->it_size = size >> tbl->it_page_shift;
  1047. /* offset for VIO should always be 0 */
  1048. tbl->it_offset = offset >> tbl->it_page_shift;
  1049. tbl->it_busno = 0;
  1050. tbl->it_type = TCE_VB;
  1051. tbl->it_blocksize = 16;
  1052. if (firmware_has_feature(FW_FEATURE_LPAR))
  1053. tbl->it_ops = &iommu_table_lpar_multi_ops;
  1054. else
  1055. tbl->it_ops = &iommu_table_pseries_ops;
  1056. return iommu_init_table(tbl, -1, 0, 0);
  1057. }
  1058. /**
  1059. * vio_match_device: - Tell if a VIO device has a matching
  1060. * VIO device id structure.
  1061. * @ids: array of VIO device id structures to search in
  1062. * @dev: the VIO device structure to match against
  1063. *
  1064. * Used by a driver to check whether a VIO device present in the
  1065. * system is in its list of supported devices. Returns the matching
  1066. * vio_device_id structure or NULL if there is no match.
  1067. */
  1068. static const struct vio_device_id *vio_match_device(
  1069. const struct vio_device_id *ids, const struct vio_dev *dev)
  1070. {
  1071. while (ids->type[0] != '\0') {
  1072. if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
  1073. of_device_is_compatible(dev->dev.of_node,
  1074. ids->compat))
  1075. return ids;
  1076. ids++;
  1077. }
  1078. return NULL;
  1079. }
  1080. /*
  1081. * Convert from struct device to struct vio_dev and pass to driver.
  1082. * dev->driver has already been set by generic code because vio_bus_match
  1083. * succeeded.
  1084. */
  1085. static int vio_bus_probe(struct device *dev)
  1086. {
  1087. struct vio_dev *viodev = to_vio_dev(dev);
  1088. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  1089. const struct vio_device_id *id;
  1090. int error = -ENODEV;
  1091. if (!viodrv->probe)
  1092. return error;
  1093. id = vio_match_device(viodrv->id_table, viodev);
  1094. if (id) {
  1095. memset(&viodev->cmo, 0, sizeof(viodev->cmo));
  1096. if (firmware_has_feature(FW_FEATURE_CMO)) {
  1097. error = vio_cmo_bus_probe(viodev);
  1098. if (error)
  1099. return error;
  1100. }
  1101. error = viodrv->probe(viodev, id);
  1102. if (error && firmware_has_feature(FW_FEATURE_CMO))
  1103. vio_cmo_bus_remove(viodev);
  1104. }
  1105. return error;
  1106. }
  1107. /* convert from struct device to struct vio_dev and pass to driver. */
  1108. static void vio_bus_remove(struct device *dev)
  1109. {
  1110. struct vio_dev *viodev = to_vio_dev(dev);
  1111. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  1112. struct device *devptr;
  1113. /*
  1114. * Hold a reference to the device after the remove function is called
  1115. * to allow for CMO accounting cleanup for the device.
  1116. */
  1117. devptr = get_device(dev);
  1118. if (viodrv->remove)
  1119. viodrv->remove(viodev);
  1120. if (firmware_has_feature(FW_FEATURE_CMO))
  1121. vio_cmo_bus_remove(viodev);
  1122. put_device(devptr);
  1123. }
  1124. static void vio_bus_shutdown(struct device *dev)
  1125. {
  1126. struct vio_dev *viodev = to_vio_dev(dev);
  1127. struct vio_driver *viodrv;
  1128. if (dev->driver) {
  1129. viodrv = to_vio_driver(dev->driver);
  1130. if (viodrv->shutdown)
  1131. viodrv->shutdown(viodev);
  1132. else if (kexec_in_progress)
  1133. vio_bus_remove(dev);
  1134. }
  1135. }
  1136. /**
  1137. * vio_register_driver: - Register a new vio driver
  1138. * @viodrv: The vio_driver structure to be registered.
  1139. */
  1140. int __vio_register_driver(struct vio_driver *viodrv, struct module *owner,
  1141. const char *mod_name)
  1142. {
  1143. // vio_bus_type is only initialised for pseries
  1144. if (!machine_is(pseries))
  1145. return -ENODEV;
  1146. pr_debug("%s: driver %s registering\n", __func__, viodrv->name);
  1147. /* fill in 'struct driver' fields */
  1148. viodrv->driver.name = viodrv->name;
  1149. viodrv->driver.pm = viodrv->pm;
  1150. viodrv->driver.bus = &vio_bus_type;
  1151. viodrv->driver.owner = owner;
  1152. viodrv->driver.mod_name = mod_name;
  1153. return driver_register(&viodrv->driver);
  1154. }
  1155. EXPORT_SYMBOL(__vio_register_driver);
  1156. /**
  1157. * vio_unregister_driver - Remove registration of vio driver.
  1158. * @viodrv: The vio_driver struct to be removed form registration
  1159. */
  1160. void vio_unregister_driver(struct vio_driver *viodrv)
  1161. {
  1162. driver_unregister(&viodrv->driver);
  1163. }
  1164. EXPORT_SYMBOL(vio_unregister_driver);
  1165. /* vio_dev refcount hit 0 */
  1166. static void vio_dev_release(struct device *dev)
  1167. {
  1168. struct iommu_table *tbl = get_iommu_table_base(dev);
  1169. if (tbl)
  1170. iommu_tce_table_put(tbl);
  1171. of_node_put(dev->of_node);
  1172. kfree(to_vio_dev(dev));
  1173. }
  1174. /**
  1175. * vio_register_device_node: - Register a new vio device.
  1176. * @of_node: The OF node for this device.
  1177. *
  1178. * Creates and initializes a vio_dev structure from the data in
  1179. * of_node and adds it to the list of virtual devices.
  1180. * Returns a pointer to the created vio_dev or NULL if node has
  1181. * NULL device_type or compatible fields.
  1182. */
  1183. struct vio_dev *vio_register_device_node(struct device_node *of_node)
  1184. {
  1185. struct vio_dev *viodev;
  1186. struct device_node *parent_node;
  1187. const __be32 *prop;
  1188. enum vio_dev_family family;
  1189. /*
  1190. * Determine if this node is a under the /vdevice node or under the
  1191. * /ibm,platform-facilities node. This decides the device's family.
  1192. */
  1193. parent_node = of_get_parent(of_node);
  1194. if (parent_node) {
  1195. if (of_node_is_type(parent_node, "ibm,platform-facilities"))
  1196. family = PFO;
  1197. else if (of_node_is_type(parent_node, "vdevice"))
  1198. family = VDEVICE;
  1199. else {
  1200. pr_warn("%s: parent(%pOF) of %pOFn not recognized.\n",
  1201. __func__,
  1202. parent_node,
  1203. of_node);
  1204. of_node_put(parent_node);
  1205. return NULL;
  1206. }
  1207. of_node_put(parent_node);
  1208. } else {
  1209. pr_warn("%s: could not determine the parent of node %pOFn.\n",
  1210. __func__, of_node);
  1211. return NULL;
  1212. }
  1213. if (family == PFO) {
  1214. if (of_get_property(of_node, "interrupt-controller", NULL)) {
  1215. pr_debug("%s: Skipping the interrupt controller %pOFn.\n",
  1216. __func__, of_node);
  1217. return NULL;
  1218. }
  1219. }
  1220. /* allocate a vio_dev for this node */
  1221. viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
  1222. if (viodev == NULL) {
  1223. pr_warn("%s: allocation failure for VIO device.\n", __func__);
  1224. return NULL;
  1225. }
  1226. /* we need the 'device_type' property, in order to match with drivers */
  1227. viodev->family = family;
  1228. if (viodev->family == VDEVICE) {
  1229. unsigned int unit_address;
  1230. viodev->type = of_node_get_device_type(of_node);
  1231. if (!viodev->type) {
  1232. pr_warn("%s: node %pOFn is missing the 'device_type' "
  1233. "property.\n", __func__, of_node);
  1234. goto out;
  1235. }
  1236. prop = of_get_property(of_node, "reg", NULL);
  1237. if (prop == NULL) {
  1238. pr_warn("%s: node %pOFn missing 'reg'\n",
  1239. __func__, of_node);
  1240. goto out;
  1241. }
  1242. unit_address = of_read_number(prop, 1);
  1243. dev_set_name(&viodev->dev, "%x", unit_address);
  1244. viodev->irq = irq_of_parse_and_map(of_node, 0);
  1245. viodev->unit_address = unit_address;
  1246. } else {
  1247. /* PFO devices need their resource_id for submitting COP_OPs
  1248. * This is an optional field for devices, but is required when
  1249. * performing synchronous ops */
  1250. prop = of_get_property(of_node, "ibm,resource-id", NULL);
  1251. if (prop != NULL)
  1252. viodev->resource_id = of_read_number(prop, 1);
  1253. dev_set_name(&viodev->dev, "%pOFn", of_node);
  1254. viodev->type = dev_name(&viodev->dev);
  1255. viodev->irq = 0;
  1256. }
  1257. viodev->name = of_node->name;
  1258. viodev->dev.of_node = of_node_get(of_node);
  1259. set_dev_node(&viodev->dev, of_node_to_nid(of_node));
  1260. /* init generic 'struct device' fields: */
  1261. viodev->dev.parent = &vio_bus_device.dev;
  1262. viodev->dev.bus = &vio_bus_type;
  1263. viodev->dev.release = vio_dev_release;
  1264. if (of_get_property(viodev->dev.of_node, "ibm,my-dma-window", NULL)) {
  1265. if (firmware_has_feature(FW_FEATURE_CMO))
  1266. vio_cmo_set_dma_ops(viodev);
  1267. else
  1268. set_dma_ops(&viodev->dev, &dma_iommu_ops);
  1269. set_iommu_table_base(&viodev->dev,
  1270. vio_build_iommu_table(viodev));
  1271. /* needed to ensure proper operation of coherent allocations
  1272. * later, in case driver doesn't set it explicitly */
  1273. viodev->dev.coherent_dma_mask = DMA_BIT_MASK(64);
  1274. viodev->dev.dma_mask = &viodev->dev.coherent_dma_mask;
  1275. }
  1276. /* register with generic device framework */
  1277. if (device_register(&viodev->dev)) {
  1278. printk(KERN_ERR "%s: failed to register device %s\n",
  1279. __func__, dev_name(&viodev->dev));
  1280. put_device(&viodev->dev);
  1281. return NULL;
  1282. }
  1283. return viodev;
  1284. out: /* Use this exit point for any return prior to device_register */
  1285. kfree(viodev);
  1286. return NULL;
  1287. }
  1288. EXPORT_SYMBOL(vio_register_device_node);
  1289. /*
  1290. * vio_bus_scan_for_devices - Scan OF and register each child device
  1291. * @root_name - OF node name for the root of the subtree to search.
  1292. * This must be non-NULL
  1293. *
  1294. * Starting from the root node provide, register the device node for
  1295. * each child beneath the root.
  1296. */
  1297. static void __init vio_bus_scan_register_devices(char *root_name)
  1298. {
  1299. struct device_node *node_root, *node_child;
  1300. if (!root_name)
  1301. return;
  1302. node_root = of_find_node_by_name(NULL, root_name);
  1303. if (node_root) {
  1304. /*
  1305. * Create struct vio_devices for each virtual device in
  1306. * the device tree. Drivers will associate with them later.
  1307. */
  1308. node_child = of_get_next_child(node_root, NULL);
  1309. while (node_child) {
  1310. vio_register_device_node(node_child);
  1311. node_child = of_get_next_child(node_root, node_child);
  1312. }
  1313. of_node_put(node_root);
  1314. }
  1315. }
  1316. /**
  1317. * vio_bus_init: - Initialize the virtual IO bus
  1318. */
  1319. static int __init vio_bus_init(void)
  1320. {
  1321. int err;
  1322. if (firmware_has_feature(FW_FEATURE_CMO))
  1323. vio_cmo_sysfs_init();
  1324. err = bus_register(&vio_bus_type);
  1325. if (err) {
  1326. printk(KERN_ERR "failed to register VIO bus\n");
  1327. return err;
  1328. }
  1329. /*
  1330. * The fake parent of all vio devices, just to give us
  1331. * a nice directory
  1332. */
  1333. err = device_register(&vio_bus_device.dev);
  1334. if (err) {
  1335. printk(KERN_WARNING "%s: device_register returned %i\n",
  1336. __func__, err);
  1337. return err;
  1338. }
  1339. if (firmware_has_feature(FW_FEATURE_CMO))
  1340. vio_cmo_bus_init();
  1341. return 0;
  1342. }
  1343. machine_postcore_initcall(pseries, vio_bus_init);
  1344. static int __init vio_device_init(void)
  1345. {
  1346. vio_bus_scan_register_devices("vdevice");
  1347. vio_bus_scan_register_devices("ibm,platform-facilities");
  1348. return 0;
  1349. }
  1350. machine_device_initcall(pseries, vio_device_init);
  1351. static ssize_t name_show(struct device *dev,
  1352. struct device_attribute *attr, char *buf)
  1353. {
  1354. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  1355. }
  1356. static DEVICE_ATTR_RO(name);
  1357. static ssize_t devspec_show(struct device *dev,
  1358. struct device_attribute *attr, char *buf)
  1359. {
  1360. struct device_node *of_node = dev->of_node;
  1361. return sprintf(buf, "%pOF\n", of_node);
  1362. }
  1363. static DEVICE_ATTR_RO(devspec);
  1364. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  1365. char *buf)
  1366. {
  1367. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1368. struct device_node *dn;
  1369. const char *cp;
  1370. dn = dev->of_node;
  1371. if (!dn) {
  1372. strcpy(buf, "\n");
  1373. return strlen(buf);
  1374. }
  1375. cp = of_get_property(dn, "compatible", NULL);
  1376. if (!cp) {
  1377. strcpy(buf, "\n");
  1378. return strlen(buf);
  1379. }
  1380. return sprintf(buf, "vio:T%sS%s\n", vio_dev->type, cp);
  1381. }
  1382. static DEVICE_ATTR_RO(modalias);
  1383. static struct attribute *vio_dev_attrs[] = {
  1384. &dev_attr_name.attr,
  1385. &dev_attr_devspec.attr,
  1386. &dev_attr_modalias.attr,
  1387. NULL,
  1388. };
  1389. ATTRIBUTE_GROUPS(vio_dev);
  1390. void vio_unregister_device(struct vio_dev *viodev)
  1391. {
  1392. device_unregister(&viodev->dev);
  1393. if (viodev->family == VDEVICE)
  1394. irq_dispose_mapping(viodev->irq);
  1395. }
  1396. EXPORT_SYMBOL(vio_unregister_device);
  1397. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  1398. {
  1399. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1400. struct vio_driver *vio_drv = to_vio_driver(drv);
  1401. const struct vio_device_id *ids = vio_drv->id_table;
  1402. return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
  1403. }
  1404. static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
  1405. {
  1406. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1407. struct device_node *dn;
  1408. const char *cp;
  1409. dn = dev->of_node;
  1410. if (!dn)
  1411. return -ENODEV;
  1412. cp = of_get_property(dn, "compatible", NULL);
  1413. if (!cp)
  1414. return -ENODEV;
  1415. add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, cp);
  1416. return 0;
  1417. }
  1418. struct bus_type vio_bus_type = {
  1419. .name = "vio",
  1420. .dev_groups = vio_dev_groups,
  1421. .uevent = vio_hotplug,
  1422. .match = vio_bus_match,
  1423. .probe = vio_bus_probe,
  1424. .remove = vio_bus_remove,
  1425. .shutdown = vio_bus_shutdown,
  1426. };
  1427. /**
  1428. * vio_get_attribute: - get attribute for virtual device
  1429. * @vdev: The vio device to get property.
  1430. * @which: The property/attribute to be extracted.
  1431. * @length: Pointer to length of returned data size (unused if NULL).
  1432. *
  1433. * Calls prom.c's of_get_property() to return the value of the
  1434. * attribute specified by @which
  1435. */
  1436. const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
  1437. {
  1438. return of_get_property(vdev->dev.of_node, which, length);
  1439. }
  1440. EXPORT_SYMBOL(vio_get_attribute);
  1441. /* vio_find_name() - internal because only vio.c knows how we formatted the
  1442. * kobject name
  1443. */
  1444. static struct vio_dev *vio_find_name(const char *name)
  1445. {
  1446. struct device *found;
  1447. found = bus_find_device_by_name(&vio_bus_type, NULL, name);
  1448. if (!found)
  1449. return NULL;
  1450. return to_vio_dev(found);
  1451. }
  1452. /**
  1453. * vio_find_node - find an already-registered vio_dev
  1454. * @vnode: device_node of the virtual device we're looking for
  1455. *
  1456. * Takes a reference to the embedded struct device which needs to be dropped
  1457. * after use.
  1458. */
  1459. struct vio_dev *vio_find_node(struct device_node *vnode)
  1460. {
  1461. char kobj_name[20];
  1462. struct device_node *vnode_parent;
  1463. vnode_parent = of_get_parent(vnode);
  1464. if (!vnode_parent)
  1465. return NULL;
  1466. /* construct the kobject name from the device node */
  1467. if (of_node_is_type(vnode_parent, "vdevice")) {
  1468. const __be32 *prop;
  1469. prop = of_get_property(vnode, "reg", NULL);
  1470. if (!prop)
  1471. goto out;
  1472. snprintf(kobj_name, sizeof(kobj_name), "%x",
  1473. (uint32_t)of_read_number(prop, 1));
  1474. } else if (of_node_is_type(vnode_parent, "ibm,platform-facilities"))
  1475. snprintf(kobj_name, sizeof(kobj_name), "%pOFn", vnode);
  1476. else
  1477. goto out;
  1478. of_node_put(vnode_parent);
  1479. return vio_find_name(kobj_name);
  1480. out:
  1481. of_node_put(vnode_parent);
  1482. return NULL;
  1483. }
  1484. EXPORT_SYMBOL(vio_find_node);
  1485. int vio_enable_interrupts(struct vio_dev *dev)
  1486. {
  1487. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
  1488. if (rc != H_SUCCESS)
  1489. printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
  1490. return rc;
  1491. }
  1492. EXPORT_SYMBOL(vio_enable_interrupts);
  1493. int vio_disable_interrupts(struct vio_dev *dev)
  1494. {
  1495. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
  1496. if (rc != H_SUCCESS)
  1497. printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
  1498. return rc;
  1499. }
  1500. EXPORT_SYMBOL(vio_disable_interrupts);
  1501. static int __init vio_init(void)
  1502. {
  1503. dma_debug_add_bus(&vio_bus_type);
  1504. return 0;
  1505. }
  1506. machine_fs_initcall(pseries, vio_init);