vio.c 48 KB

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