vio.c 47 KB

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