vio.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  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. .map_sg = vio_dma_iommu_map_sg,
  533. .unmap_sg = vio_dma_iommu_unmap_sg,
  534. .map_page = vio_dma_iommu_map_page,
  535. .unmap_page = vio_dma_iommu_unmap_page,
  536. .dma_supported = vio_dma_iommu_dma_supported,
  537. .get_required_mask = vio_dma_get_required_mask,
  538. };
  539. /**
  540. * vio_cmo_set_dev_desired - Set desired entitlement for a device
  541. *
  542. * @viodev: struct vio_dev for device to alter
  543. * @new_desired: new desired entitlement level in bytes
  544. *
  545. * For use by devices to request a change to their entitlement at runtime or
  546. * through sysfs. The desired entitlement level is changed and a balancing
  547. * of system resources is scheduled to run in the future.
  548. */
  549. void vio_cmo_set_dev_desired(struct vio_dev *viodev, size_t desired)
  550. {
  551. unsigned long flags;
  552. struct vio_cmo_dev_entry *dev_ent;
  553. int found = 0;
  554. if (!firmware_has_feature(FW_FEATURE_CMO))
  555. return;
  556. spin_lock_irqsave(&vio_cmo.lock, flags);
  557. if (desired < VIO_CMO_MIN_ENT)
  558. desired = VIO_CMO_MIN_ENT;
  559. /*
  560. * Changes will not be made for devices not in the device list.
  561. * If it is not in the device list, then no driver is loaded
  562. * for the device and it can not receive entitlement.
  563. */
  564. list_for_each_entry(dev_ent, &vio_cmo.device_list, list)
  565. if (viodev == dev_ent->viodev) {
  566. found = 1;
  567. break;
  568. }
  569. if (!found) {
  570. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  571. return;
  572. }
  573. /* Increase/decrease in desired device entitlement */
  574. if (desired >= viodev->cmo.desired) {
  575. /* Just bump the bus and device values prior to a balance*/
  576. vio_cmo.desired += desired - viodev->cmo.desired;
  577. viodev->cmo.desired = desired;
  578. } else {
  579. /* Decrease bus and device values for desired entitlement */
  580. vio_cmo.desired -= viodev->cmo.desired - desired;
  581. viodev->cmo.desired = desired;
  582. /*
  583. * If less entitlement is desired than current entitlement, move
  584. * any reserve memory in the change region to the excess pool.
  585. */
  586. if (viodev->cmo.entitled > desired) {
  587. vio_cmo.reserve.size -= viodev->cmo.entitled - desired;
  588. vio_cmo.excess.size += viodev->cmo.entitled - desired;
  589. /*
  590. * If entitlement moving from the reserve pool to the
  591. * excess pool is currently unused, add to the excess
  592. * free counter.
  593. */
  594. if (viodev->cmo.allocated < viodev->cmo.entitled)
  595. vio_cmo.excess.free += viodev->cmo.entitled -
  596. max(viodev->cmo.allocated, desired);
  597. viodev->cmo.entitled = desired;
  598. }
  599. }
  600. schedule_delayed_work(&vio_cmo.balance_q, 0);
  601. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  602. }
  603. /**
  604. * vio_cmo_bus_probe - Handle CMO specific bus probe activities
  605. *
  606. * @viodev - Pointer to struct vio_dev for device
  607. *
  608. * Determine the devices IO memory entitlement needs, attempting
  609. * to satisfy the system minimum entitlement at first and scheduling
  610. * a balance operation to take care of the rest at a later time.
  611. *
  612. * Returns: 0 on success, -EINVAL when device doesn't support CMO, and
  613. * -ENOMEM when entitlement is not available for device or
  614. * device entry.
  615. *
  616. */
  617. static int vio_cmo_bus_probe(struct vio_dev *viodev)
  618. {
  619. struct vio_cmo_dev_entry *dev_ent;
  620. struct device *dev = &viodev->dev;
  621. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  622. unsigned long flags;
  623. size_t size;
  624. bool dma_capable = false;
  625. /* A device requires entitlement if it has a DMA window property */
  626. switch (viodev->family) {
  627. case VDEVICE:
  628. if (of_get_property(viodev->dev.of_node,
  629. "ibm,my-dma-window", NULL))
  630. dma_capable = true;
  631. break;
  632. case PFO:
  633. dma_capable = false;
  634. break;
  635. default:
  636. dev_warn(dev, "unknown device family: %d\n", viodev->family);
  637. BUG();
  638. break;
  639. }
  640. /* Configure entitlement for the device. */
  641. if (dma_capable) {
  642. /* Check that the driver is CMO enabled and get desired DMA */
  643. if (!viodrv->get_desired_dma) {
  644. dev_err(dev, "%s: device driver does not support CMO\n",
  645. __func__);
  646. return -EINVAL;
  647. }
  648. viodev->cmo.desired = IOMMU_PAGE_ALIGN(viodrv->get_desired_dma(viodev));
  649. if (viodev->cmo.desired < VIO_CMO_MIN_ENT)
  650. viodev->cmo.desired = VIO_CMO_MIN_ENT;
  651. size = VIO_CMO_MIN_ENT;
  652. dev_ent = kmalloc(sizeof(struct vio_cmo_dev_entry),
  653. GFP_KERNEL);
  654. if (!dev_ent)
  655. return -ENOMEM;
  656. dev_ent->viodev = viodev;
  657. spin_lock_irqsave(&vio_cmo.lock, flags);
  658. list_add(&dev_ent->list, &vio_cmo.device_list);
  659. } else {
  660. viodev->cmo.desired = 0;
  661. size = 0;
  662. spin_lock_irqsave(&vio_cmo.lock, flags);
  663. }
  664. /*
  665. * If the needs for vio_cmo.min have not changed since they
  666. * were last set, the number of devices in the OF tree has
  667. * been constant and the IO memory for this is already in
  668. * the reserve pool.
  669. */
  670. if (vio_cmo.min == ((vio_cmo_num_OF_devs() + 1) *
  671. VIO_CMO_MIN_ENT)) {
  672. /* Updated desired entitlement if device requires it */
  673. if (size)
  674. vio_cmo.desired += (viodev->cmo.desired -
  675. VIO_CMO_MIN_ENT);
  676. } else {
  677. size_t tmp;
  678. tmp = vio_cmo.spare + vio_cmo.excess.free;
  679. if (tmp < size) {
  680. dev_err(dev, "%s: insufficient free "
  681. "entitlement to add device. "
  682. "Need %lu, have %lu\n", __func__,
  683. size, (vio_cmo.spare + tmp));
  684. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  685. return -ENOMEM;
  686. }
  687. /* Use excess pool first to fulfill request */
  688. tmp = min(size, vio_cmo.excess.free);
  689. vio_cmo.excess.free -= tmp;
  690. vio_cmo.excess.size -= tmp;
  691. vio_cmo.reserve.size += tmp;
  692. /* Use spare if excess pool was insufficient */
  693. vio_cmo.spare -= size - tmp;
  694. /* Update bus accounting */
  695. vio_cmo.min += size;
  696. vio_cmo.desired += viodev->cmo.desired;
  697. }
  698. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  699. return 0;
  700. }
  701. /**
  702. * vio_cmo_bus_remove - Handle CMO specific bus removal activities
  703. *
  704. * @viodev - Pointer to struct vio_dev for device
  705. *
  706. * Remove the device from the cmo device list. The minimum entitlement
  707. * will be reserved for the device as long as it is in the system. The
  708. * rest of the entitlement the device had been allocated will be returned
  709. * to the system.
  710. */
  711. static void vio_cmo_bus_remove(struct vio_dev *viodev)
  712. {
  713. struct vio_cmo_dev_entry *dev_ent;
  714. unsigned long flags;
  715. size_t tmp;
  716. spin_lock_irqsave(&vio_cmo.lock, flags);
  717. if (viodev->cmo.allocated) {
  718. dev_err(&viodev->dev, "%s: device had %lu bytes of IO "
  719. "allocated after remove operation.\n",
  720. __func__, viodev->cmo.allocated);
  721. BUG();
  722. }
  723. /*
  724. * Remove the device from the device list being maintained for
  725. * CMO enabled devices.
  726. */
  727. list_for_each_entry(dev_ent, &vio_cmo.device_list, list)
  728. if (viodev == dev_ent->viodev) {
  729. list_del(&dev_ent->list);
  730. kfree(dev_ent);
  731. break;
  732. }
  733. /*
  734. * Devices may not require any entitlement and they do not need
  735. * to be processed. Otherwise, return the device's entitlement
  736. * back to the pools.
  737. */
  738. if (viodev->cmo.entitled) {
  739. /*
  740. * This device has not yet left the OF tree, it's
  741. * minimum entitlement remains in vio_cmo.min and
  742. * vio_cmo.desired
  743. */
  744. vio_cmo.desired -= (viodev->cmo.desired - VIO_CMO_MIN_ENT);
  745. /*
  746. * Save min allocation for device in reserve as long
  747. * as it exists in OF tree as determined by later
  748. * balance operation
  749. */
  750. viodev->cmo.entitled -= VIO_CMO_MIN_ENT;
  751. /* Replenish spare from freed reserve pool */
  752. if (viodev->cmo.entitled && (vio_cmo.spare < VIO_CMO_MIN_ENT)) {
  753. tmp = min(viodev->cmo.entitled, (VIO_CMO_MIN_ENT -
  754. vio_cmo.spare));
  755. vio_cmo.spare += tmp;
  756. viodev->cmo.entitled -= tmp;
  757. }
  758. /* Remaining reserve goes to excess pool */
  759. vio_cmo.excess.size += viodev->cmo.entitled;
  760. vio_cmo.excess.free += viodev->cmo.entitled;
  761. vio_cmo.reserve.size -= viodev->cmo.entitled;
  762. /*
  763. * Until the device is removed it will keep a
  764. * minimum entitlement; this will guarantee that
  765. * a module unload/load will result in a success.
  766. */
  767. viodev->cmo.entitled = VIO_CMO_MIN_ENT;
  768. viodev->cmo.desired = VIO_CMO_MIN_ENT;
  769. atomic_set(&viodev->cmo.allocs_failed, 0);
  770. }
  771. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  772. }
  773. static void vio_cmo_set_dma_ops(struct vio_dev *viodev)
  774. {
  775. set_dma_ops(&viodev->dev, &vio_dma_mapping_ops);
  776. }
  777. /**
  778. * vio_cmo_bus_init - CMO entitlement initialization at bus init time
  779. *
  780. * Set up the reserve and excess entitlement pools based on available
  781. * system entitlement and the number of devices in the OF tree that
  782. * require entitlement in the reserve pool.
  783. */
  784. static void vio_cmo_bus_init(void)
  785. {
  786. struct hvcall_mpp_data mpp_data;
  787. int err;
  788. memset(&vio_cmo, 0, sizeof(struct vio_cmo));
  789. spin_lock_init(&vio_cmo.lock);
  790. INIT_LIST_HEAD(&vio_cmo.device_list);
  791. INIT_DELAYED_WORK(&vio_cmo.balance_q, vio_cmo_balance);
  792. /* Get current system entitlement */
  793. err = h_get_mpp(&mpp_data);
  794. /*
  795. * On failure, continue with entitlement set to 0, will panic()
  796. * later when spare is reserved.
  797. */
  798. if (err != H_SUCCESS) {
  799. printk(KERN_ERR "%s: unable to determine system IO "\
  800. "entitlement. (%d)\n", __func__, err);
  801. vio_cmo.entitled = 0;
  802. } else {
  803. vio_cmo.entitled = mpp_data.entitled_mem;
  804. }
  805. /* Set reservation and check against entitlement */
  806. vio_cmo.spare = VIO_CMO_MIN_ENT;
  807. vio_cmo.reserve.size = vio_cmo.spare;
  808. vio_cmo.reserve.size += (vio_cmo_num_OF_devs() *
  809. VIO_CMO_MIN_ENT);
  810. if (vio_cmo.reserve.size > vio_cmo.entitled) {
  811. printk(KERN_ERR "%s: insufficient system entitlement\n",
  812. __func__);
  813. panic("%s: Insufficient system entitlement", __func__);
  814. }
  815. /* Set the remaining accounting variables */
  816. vio_cmo.excess.size = vio_cmo.entitled - vio_cmo.reserve.size;
  817. vio_cmo.excess.free = vio_cmo.excess.size;
  818. vio_cmo.min = vio_cmo.reserve.size;
  819. vio_cmo.desired = vio_cmo.reserve.size;
  820. }
  821. /* sysfs device functions and data structures for CMO */
  822. #define viodev_cmo_rd_attr(name) \
  823. static ssize_t viodev_cmo_##name##_show(struct device *dev, \
  824. struct device_attribute *attr, \
  825. char *buf) \
  826. { \
  827. return sprintf(buf, "%lu\n", to_vio_dev(dev)->cmo.name); \
  828. }
  829. static ssize_t viodev_cmo_allocs_failed_show(struct device *dev,
  830. struct device_attribute *attr, char *buf)
  831. {
  832. struct vio_dev *viodev = to_vio_dev(dev);
  833. return sprintf(buf, "%d\n", atomic_read(&viodev->cmo.allocs_failed));
  834. }
  835. static ssize_t viodev_cmo_allocs_failed_reset(struct device *dev,
  836. struct device_attribute *attr, const char *buf, size_t count)
  837. {
  838. struct vio_dev *viodev = to_vio_dev(dev);
  839. atomic_set(&viodev->cmo.allocs_failed, 0);
  840. return count;
  841. }
  842. static ssize_t viodev_cmo_desired_set(struct device *dev,
  843. struct device_attribute *attr, const char *buf, size_t count)
  844. {
  845. struct vio_dev *viodev = to_vio_dev(dev);
  846. size_t new_desired;
  847. int ret;
  848. ret = strict_strtoul(buf, 10, &new_desired);
  849. if (ret)
  850. return ret;
  851. vio_cmo_set_dev_desired(viodev, new_desired);
  852. return count;
  853. }
  854. viodev_cmo_rd_attr(desired);
  855. viodev_cmo_rd_attr(entitled);
  856. viodev_cmo_rd_attr(allocated);
  857. static ssize_t name_show(struct device *, struct device_attribute *, char *);
  858. static ssize_t devspec_show(struct device *, struct device_attribute *, char *);
  859. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  860. char *buf);
  861. static struct device_attribute vio_cmo_dev_attrs[] = {
  862. __ATTR_RO(name),
  863. __ATTR_RO(devspec),
  864. __ATTR_RO(modalias),
  865. __ATTR(cmo_desired, S_IWUSR|S_IRUSR|S_IWGRP|S_IRGRP|S_IROTH,
  866. viodev_cmo_desired_show, viodev_cmo_desired_set),
  867. __ATTR(cmo_entitled, S_IRUGO, viodev_cmo_entitled_show, NULL),
  868. __ATTR(cmo_allocated, S_IRUGO, viodev_cmo_allocated_show, NULL),
  869. __ATTR(cmo_allocs_failed, S_IWUSR|S_IRUSR|S_IWGRP|S_IRGRP|S_IROTH,
  870. viodev_cmo_allocs_failed_show, viodev_cmo_allocs_failed_reset),
  871. __ATTR_NULL
  872. };
  873. /* sysfs bus functions and data structures for CMO */
  874. #define viobus_cmo_rd_attr(name) \
  875. static ssize_t \
  876. viobus_cmo_##name##_show(struct bus_type *bt, char *buf) \
  877. { \
  878. return sprintf(buf, "%lu\n", vio_cmo.name); \
  879. }
  880. #define viobus_cmo_pool_rd_attr(name, var) \
  881. static ssize_t \
  882. viobus_cmo_##name##_pool_show_##var(struct bus_type *bt, char *buf) \
  883. { \
  884. return sprintf(buf, "%lu\n", vio_cmo.name.var); \
  885. }
  886. static ssize_t viobus_cmo_high_reset(struct bus_type *bt, const char *buf,
  887. size_t count)
  888. {
  889. unsigned long flags;
  890. spin_lock_irqsave(&vio_cmo.lock, flags);
  891. vio_cmo.high = vio_cmo.curr;
  892. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  893. return count;
  894. }
  895. viobus_cmo_rd_attr(entitled);
  896. viobus_cmo_pool_rd_attr(reserve, size);
  897. viobus_cmo_pool_rd_attr(excess, size);
  898. viobus_cmo_pool_rd_attr(excess, free);
  899. viobus_cmo_rd_attr(spare);
  900. viobus_cmo_rd_attr(min);
  901. viobus_cmo_rd_attr(desired);
  902. viobus_cmo_rd_attr(curr);
  903. viobus_cmo_rd_attr(high);
  904. static struct bus_attribute vio_cmo_bus_attrs[] = {
  905. __ATTR(cmo_entitled, S_IRUGO, viobus_cmo_entitled_show, NULL),
  906. __ATTR(cmo_reserve_size, S_IRUGO, viobus_cmo_reserve_pool_show_size, NULL),
  907. __ATTR(cmo_excess_size, S_IRUGO, viobus_cmo_excess_pool_show_size, NULL),
  908. __ATTR(cmo_excess_free, S_IRUGO, viobus_cmo_excess_pool_show_free, NULL),
  909. __ATTR(cmo_spare, S_IRUGO, viobus_cmo_spare_show, NULL),
  910. __ATTR(cmo_min, S_IRUGO, viobus_cmo_min_show, NULL),
  911. __ATTR(cmo_desired, S_IRUGO, viobus_cmo_desired_show, NULL),
  912. __ATTR(cmo_curr, S_IRUGO, viobus_cmo_curr_show, NULL),
  913. __ATTR(cmo_high, S_IWUSR|S_IRUSR|S_IWGRP|S_IRGRP|S_IROTH,
  914. viobus_cmo_high_show, viobus_cmo_high_reset),
  915. __ATTR_NULL
  916. };
  917. static void vio_cmo_sysfs_init(void)
  918. {
  919. vio_bus_type.dev_attrs = vio_cmo_dev_attrs;
  920. vio_bus_type.bus_attrs = vio_cmo_bus_attrs;
  921. }
  922. #else /* CONFIG_PPC_SMLPAR */
  923. int vio_cmo_entitlement_update(size_t new_entitlement) { return 0; }
  924. void vio_cmo_set_dev_desired(struct vio_dev *viodev, size_t desired) {}
  925. static int vio_cmo_bus_probe(struct vio_dev *viodev) { return 0; }
  926. static void vio_cmo_bus_remove(struct vio_dev *viodev) {}
  927. static void vio_cmo_set_dma_ops(struct vio_dev *viodev) {}
  928. static void vio_cmo_bus_init(void) {}
  929. static void vio_cmo_sysfs_init(void) { }
  930. #endif /* CONFIG_PPC_SMLPAR */
  931. EXPORT_SYMBOL(vio_cmo_entitlement_update);
  932. EXPORT_SYMBOL(vio_cmo_set_dev_desired);
  933. /*
  934. * Platform Facilities Option (PFO) support
  935. */
  936. /**
  937. * vio_h_cop_sync - Perform a synchronous PFO co-processor operation
  938. *
  939. * @vdev - Pointer to a struct vio_dev for device
  940. * @op - Pointer to a struct vio_pfo_op for the operation parameters
  941. *
  942. * Calls the hypervisor to synchronously perform the PFO operation
  943. * described in @op. In the case of a busy response from the hypervisor,
  944. * the operation will be re-submitted indefinitely unless a non-zero timeout
  945. * is specified or an error occurs. The timeout places a limit on when to
  946. * stop re-submitting a operation, the total time can be exceeded if an
  947. * operation is in progress.
  948. *
  949. * If op->hcall_ret is not NULL, this will be set to the return from the
  950. * last h_cop_op call or it will be 0 if an error not involving the h_call
  951. * was encountered.
  952. *
  953. * Returns:
  954. * 0 on success,
  955. * -EINVAL if the h_call fails due to an invalid parameter,
  956. * -E2BIG if the h_call can not be performed synchronously,
  957. * -EBUSY if a timeout is specified and has elapsed,
  958. * -EACCES if the memory area for data/status has been rescinded, or
  959. * -EPERM if a hardware fault has been indicated
  960. */
  961. int vio_h_cop_sync(struct vio_dev *vdev, struct vio_pfo_op *op)
  962. {
  963. struct device *dev = &vdev->dev;
  964. unsigned long deadline = 0;
  965. long hret = 0;
  966. int ret = 0;
  967. if (op->timeout)
  968. deadline = jiffies + msecs_to_jiffies(op->timeout);
  969. while (true) {
  970. hret = plpar_hcall_norets(H_COP, op->flags,
  971. vdev->resource_id,
  972. op->in, op->inlen, op->out,
  973. op->outlen, op->csbcpb);
  974. if (hret == H_SUCCESS ||
  975. (hret != H_NOT_ENOUGH_RESOURCES &&
  976. hret != H_BUSY && hret != H_RESOURCE) ||
  977. (op->timeout && time_after(deadline, jiffies)))
  978. break;
  979. dev_dbg(dev, "%s: hcall ret(%ld), retrying.\n", __func__, hret);
  980. }
  981. switch (hret) {
  982. case H_SUCCESS:
  983. ret = 0;
  984. break;
  985. case H_OP_MODE:
  986. case H_TOO_BIG:
  987. ret = -E2BIG;
  988. break;
  989. case H_RESCINDED:
  990. ret = -EACCES;
  991. break;
  992. case H_HARDWARE:
  993. ret = -EPERM;
  994. break;
  995. case H_NOT_ENOUGH_RESOURCES:
  996. case H_RESOURCE:
  997. case H_BUSY:
  998. ret = -EBUSY;
  999. break;
  1000. default:
  1001. ret = -EINVAL;
  1002. break;
  1003. }
  1004. if (ret)
  1005. dev_dbg(dev, "%s: Sync h_cop_op failure (ret:%d) (hret:%ld)\n",
  1006. __func__, ret, hret);
  1007. op->hcall_err = hret;
  1008. return ret;
  1009. }
  1010. EXPORT_SYMBOL(vio_h_cop_sync);
  1011. static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
  1012. {
  1013. const unsigned char *dma_window;
  1014. struct iommu_table *tbl;
  1015. unsigned long offset, size;
  1016. dma_window = of_get_property(dev->dev.of_node,
  1017. "ibm,my-dma-window", NULL);
  1018. if (!dma_window)
  1019. return NULL;
  1020. tbl = kzalloc(sizeof(*tbl), GFP_KERNEL);
  1021. if (tbl == NULL)
  1022. return NULL;
  1023. of_parse_dma_window(dev->dev.of_node, dma_window,
  1024. &tbl->it_index, &offset, &size);
  1025. /* TCE table size - measured in tce entries */
  1026. tbl->it_size = size >> IOMMU_PAGE_SHIFT;
  1027. /* offset for VIO should always be 0 */
  1028. tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
  1029. tbl->it_busno = 0;
  1030. tbl->it_type = TCE_VB;
  1031. tbl->it_blocksize = 16;
  1032. return iommu_init_table(tbl, -1);
  1033. }
  1034. /**
  1035. * vio_match_device: - Tell if a VIO device has a matching
  1036. * VIO device id structure.
  1037. * @ids: array of VIO device id structures to search in
  1038. * @dev: the VIO device structure to match against
  1039. *
  1040. * Used by a driver to check whether a VIO device present in the
  1041. * system is in its list of supported devices. Returns the matching
  1042. * vio_device_id structure or NULL if there is no match.
  1043. */
  1044. static const struct vio_device_id *vio_match_device(
  1045. const struct vio_device_id *ids, const struct vio_dev *dev)
  1046. {
  1047. while (ids->type[0] != '\0') {
  1048. if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
  1049. of_device_is_compatible(dev->dev.of_node,
  1050. ids->compat))
  1051. return ids;
  1052. ids++;
  1053. }
  1054. return NULL;
  1055. }
  1056. /*
  1057. * Convert from struct device to struct vio_dev and pass to driver.
  1058. * dev->driver has already been set by generic code because vio_bus_match
  1059. * succeeded.
  1060. */
  1061. static int vio_bus_probe(struct device *dev)
  1062. {
  1063. struct vio_dev *viodev = to_vio_dev(dev);
  1064. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  1065. const struct vio_device_id *id;
  1066. int error = -ENODEV;
  1067. if (!viodrv->probe)
  1068. return error;
  1069. id = vio_match_device(viodrv->id_table, viodev);
  1070. if (id) {
  1071. memset(&viodev->cmo, 0, sizeof(viodev->cmo));
  1072. if (firmware_has_feature(FW_FEATURE_CMO)) {
  1073. error = vio_cmo_bus_probe(viodev);
  1074. if (error)
  1075. return error;
  1076. }
  1077. error = viodrv->probe(viodev, id);
  1078. if (error && firmware_has_feature(FW_FEATURE_CMO))
  1079. vio_cmo_bus_remove(viodev);
  1080. }
  1081. return error;
  1082. }
  1083. /* convert from struct device to struct vio_dev and pass to driver. */
  1084. static int vio_bus_remove(struct device *dev)
  1085. {
  1086. struct vio_dev *viodev = to_vio_dev(dev);
  1087. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  1088. struct device *devptr;
  1089. int ret = 1;
  1090. /*
  1091. * Hold a reference to the device after the remove function is called
  1092. * to allow for CMO accounting cleanup for the device.
  1093. */
  1094. devptr = get_device(dev);
  1095. if (viodrv->remove)
  1096. ret = viodrv->remove(viodev);
  1097. if (!ret && firmware_has_feature(FW_FEATURE_CMO))
  1098. vio_cmo_bus_remove(viodev);
  1099. put_device(devptr);
  1100. return ret;
  1101. }
  1102. /**
  1103. * vio_register_driver: - Register a new vio driver
  1104. * @drv: The vio_driver structure to be registered.
  1105. */
  1106. int __vio_register_driver(struct vio_driver *viodrv, struct module *owner,
  1107. const char *mod_name)
  1108. {
  1109. pr_debug("%s: driver %s registering\n", __func__, viodrv->name);
  1110. /* fill in 'struct driver' fields */
  1111. viodrv->driver.name = viodrv->name;
  1112. viodrv->driver.pm = viodrv->pm;
  1113. viodrv->driver.bus = &vio_bus_type;
  1114. viodrv->driver.owner = owner;
  1115. viodrv->driver.mod_name = mod_name;
  1116. return driver_register(&viodrv->driver);
  1117. }
  1118. EXPORT_SYMBOL(__vio_register_driver);
  1119. /**
  1120. * vio_unregister_driver - Remove registration of vio driver.
  1121. * @driver: The vio_driver struct to be removed form registration
  1122. */
  1123. void vio_unregister_driver(struct vio_driver *viodrv)
  1124. {
  1125. driver_unregister(&viodrv->driver);
  1126. }
  1127. EXPORT_SYMBOL(vio_unregister_driver);
  1128. /* vio_dev refcount hit 0 */
  1129. static void __devinit vio_dev_release(struct device *dev)
  1130. {
  1131. struct iommu_table *tbl = get_iommu_table_base(dev);
  1132. if (tbl)
  1133. iommu_free_table(tbl, dev->of_node ?
  1134. dev->of_node->full_name : dev_name(dev));
  1135. of_node_put(dev->of_node);
  1136. kfree(to_vio_dev(dev));
  1137. }
  1138. /**
  1139. * vio_register_device_node: - Register a new vio device.
  1140. * @of_node: The OF node for this device.
  1141. *
  1142. * Creates and initializes a vio_dev structure from the data in
  1143. * of_node and adds it to the list of virtual devices.
  1144. * Returns a pointer to the created vio_dev or NULL if node has
  1145. * NULL device_type or compatible fields.
  1146. */
  1147. struct vio_dev *vio_register_device_node(struct device_node *of_node)
  1148. {
  1149. struct vio_dev *viodev;
  1150. struct device_node *parent_node;
  1151. const unsigned int *unit_address;
  1152. const unsigned int *pfo_resid = NULL;
  1153. enum vio_dev_family family;
  1154. const char *of_node_name = of_node->name ? of_node->name : "<unknown>";
  1155. /*
  1156. * Determine if this node is a under the /vdevice node or under the
  1157. * /ibm,platform-facilities node. This decides the device's family.
  1158. */
  1159. parent_node = of_get_parent(of_node);
  1160. if (parent_node) {
  1161. if (!strcmp(parent_node->full_name, "/ibm,platform-facilities"))
  1162. family = PFO;
  1163. else if (!strcmp(parent_node->full_name, "/vdevice"))
  1164. family = VDEVICE;
  1165. else {
  1166. pr_warn("%s: parent(%s) of %s not recognized.\n",
  1167. __func__,
  1168. parent_node->full_name,
  1169. of_node_name);
  1170. of_node_put(parent_node);
  1171. return NULL;
  1172. }
  1173. of_node_put(parent_node);
  1174. } else {
  1175. pr_warn("%s: could not determine the parent of node %s.\n",
  1176. __func__, of_node_name);
  1177. return NULL;
  1178. }
  1179. if (family == PFO) {
  1180. if (of_get_property(of_node, "interrupt-controller", NULL)) {
  1181. pr_debug("%s: Skipping the interrupt controller %s.\n",
  1182. __func__, of_node_name);
  1183. return NULL;
  1184. }
  1185. }
  1186. /* allocate a vio_dev for this node */
  1187. viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
  1188. if (viodev == NULL) {
  1189. pr_warn("%s: allocation failure for VIO device.\n", __func__);
  1190. return NULL;
  1191. }
  1192. /* we need the 'device_type' property, in order to match with drivers */
  1193. viodev->family = family;
  1194. if (viodev->family == VDEVICE) {
  1195. if (of_node->type != NULL)
  1196. viodev->type = of_node->type;
  1197. else {
  1198. pr_warn("%s: node %s is missing the 'device_type' "
  1199. "property.\n", __func__, of_node_name);
  1200. goto out;
  1201. }
  1202. unit_address = of_get_property(of_node, "reg", NULL);
  1203. if (unit_address == NULL) {
  1204. pr_warn("%s: node %s missing 'reg'\n",
  1205. __func__, of_node_name);
  1206. goto out;
  1207. }
  1208. dev_set_name(&viodev->dev, "%x", *unit_address);
  1209. viodev->irq = irq_of_parse_and_map(of_node, 0);
  1210. viodev->unit_address = *unit_address;
  1211. } else {
  1212. /* PFO devices need their resource_id for submitting COP_OPs
  1213. * This is an optional field for devices, but is required when
  1214. * performing synchronous ops */
  1215. pfo_resid = of_get_property(of_node, "ibm,resource-id", NULL);
  1216. if (pfo_resid != NULL)
  1217. viodev->resource_id = *pfo_resid;
  1218. unit_address = NULL;
  1219. dev_set_name(&viodev->dev, "%s", of_node_name);
  1220. viodev->type = of_node_name;
  1221. viodev->irq = 0;
  1222. }
  1223. viodev->name = of_node->name;
  1224. viodev->dev.of_node = of_node_get(of_node);
  1225. if (firmware_has_feature(FW_FEATURE_CMO))
  1226. vio_cmo_set_dma_ops(viodev);
  1227. else
  1228. set_dma_ops(&viodev->dev, &dma_iommu_ops);
  1229. set_iommu_table_base(&viodev->dev, vio_build_iommu_table(viodev));
  1230. set_dev_node(&viodev->dev, of_node_to_nid(of_node));
  1231. /* init generic 'struct device' fields: */
  1232. viodev->dev.parent = &vio_bus_device.dev;
  1233. viodev->dev.bus = &vio_bus_type;
  1234. viodev->dev.release = vio_dev_release;
  1235. /* needed to ensure proper operation of coherent allocations
  1236. * later, in case driver doesn't set it explicitly */
  1237. dma_set_mask(&viodev->dev, DMA_BIT_MASK(64));
  1238. dma_set_coherent_mask(&viodev->dev, DMA_BIT_MASK(64));
  1239. /* register with generic device framework */
  1240. if (device_register(&viodev->dev)) {
  1241. printk(KERN_ERR "%s: failed to register device %s\n",
  1242. __func__, dev_name(&viodev->dev));
  1243. put_device(&viodev->dev);
  1244. return NULL;
  1245. }
  1246. return viodev;
  1247. out: /* Use this exit point for any return prior to device_register */
  1248. kfree(viodev);
  1249. return NULL;
  1250. }
  1251. EXPORT_SYMBOL(vio_register_device_node);
  1252. /*
  1253. * vio_bus_scan_for_devices - Scan OF and register each child device
  1254. * @root_name - OF node name for the root of the subtree to search.
  1255. * This must be non-NULL
  1256. *
  1257. * Starting from the root node provide, register the device node for
  1258. * each child beneath the root.
  1259. */
  1260. static void vio_bus_scan_register_devices(char *root_name)
  1261. {
  1262. struct device_node *node_root, *node_child;
  1263. if (!root_name)
  1264. return;
  1265. node_root = of_find_node_by_name(NULL, root_name);
  1266. if (node_root) {
  1267. /*
  1268. * Create struct vio_devices for each virtual device in
  1269. * the device tree. Drivers will associate with them later.
  1270. */
  1271. node_child = of_get_next_child(node_root, NULL);
  1272. while (node_child) {
  1273. vio_register_device_node(node_child);
  1274. node_child = of_get_next_child(node_root, node_child);
  1275. }
  1276. of_node_put(node_root);
  1277. }
  1278. }
  1279. /**
  1280. * vio_bus_init: - Initialize the virtual IO bus
  1281. */
  1282. static int __init vio_bus_init(void)
  1283. {
  1284. int err;
  1285. if (firmware_has_feature(FW_FEATURE_CMO))
  1286. vio_cmo_sysfs_init();
  1287. err = bus_register(&vio_bus_type);
  1288. if (err) {
  1289. printk(KERN_ERR "failed to register VIO bus\n");
  1290. return err;
  1291. }
  1292. /*
  1293. * The fake parent of all vio devices, just to give us
  1294. * a nice directory
  1295. */
  1296. err = device_register(&vio_bus_device.dev);
  1297. if (err) {
  1298. printk(KERN_WARNING "%s: device_register returned %i\n",
  1299. __func__, err);
  1300. return err;
  1301. }
  1302. if (firmware_has_feature(FW_FEATURE_CMO))
  1303. vio_cmo_bus_init();
  1304. vio_bus_scan_register_devices("vdevice");
  1305. vio_bus_scan_register_devices("ibm,platform-facilities");
  1306. return 0;
  1307. }
  1308. __initcall(vio_bus_init);
  1309. static ssize_t name_show(struct device *dev,
  1310. struct device_attribute *attr, char *buf)
  1311. {
  1312. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  1313. }
  1314. static ssize_t devspec_show(struct device *dev,
  1315. struct device_attribute *attr, char *buf)
  1316. {
  1317. struct device_node *of_node = dev->of_node;
  1318. return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
  1319. }
  1320. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  1321. char *buf)
  1322. {
  1323. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1324. struct device_node *dn;
  1325. const char *cp;
  1326. dn = dev->of_node;
  1327. if (!dn)
  1328. return -ENODEV;
  1329. cp = of_get_property(dn, "compatible", NULL);
  1330. if (!cp)
  1331. return -ENODEV;
  1332. return sprintf(buf, "vio:T%sS%s\n", vio_dev->type, cp);
  1333. }
  1334. static struct device_attribute vio_dev_attrs[] = {
  1335. __ATTR_RO(name),
  1336. __ATTR_RO(devspec),
  1337. __ATTR_RO(modalias),
  1338. __ATTR_NULL
  1339. };
  1340. void __devinit vio_unregister_device(struct vio_dev *viodev)
  1341. {
  1342. device_unregister(&viodev->dev);
  1343. }
  1344. EXPORT_SYMBOL(vio_unregister_device);
  1345. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  1346. {
  1347. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1348. struct vio_driver *vio_drv = to_vio_driver(drv);
  1349. const struct vio_device_id *ids = vio_drv->id_table;
  1350. return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
  1351. }
  1352. static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
  1353. {
  1354. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1355. struct device_node *dn;
  1356. const char *cp;
  1357. dn = dev->of_node;
  1358. if (!dn)
  1359. return -ENODEV;
  1360. cp = of_get_property(dn, "compatible", NULL);
  1361. if (!cp)
  1362. return -ENODEV;
  1363. add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, cp);
  1364. return 0;
  1365. }
  1366. static struct bus_type vio_bus_type = {
  1367. .name = "vio",
  1368. .dev_attrs = vio_dev_attrs,
  1369. .uevent = vio_hotplug,
  1370. .match = vio_bus_match,
  1371. .probe = vio_bus_probe,
  1372. .remove = vio_bus_remove,
  1373. };
  1374. /**
  1375. * vio_get_attribute: - get attribute for virtual device
  1376. * @vdev: The vio device to get property.
  1377. * @which: The property/attribute to be extracted.
  1378. * @length: Pointer to length of returned data size (unused if NULL).
  1379. *
  1380. * Calls prom.c's of_get_property() to return the value of the
  1381. * attribute specified by @which
  1382. */
  1383. const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
  1384. {
  1385. return of_get_property(vdev->dev.of_node, which, length);
  1386. }
  1387. EXPORT_SYMBOL(vio_get_attribute);
  1388. #ifdef CONFIG_PPC_PSERIES
  1389. /* vio_find_name() - internal because only vio.c knows how we formatted the
  1390. * kobject name
  1391. */
  1392. static struct vio_dev *vio_find_name(const char *name)
  1393. {
  1394. struct device *found;
  1395. found = bus_find_device_by_name(&vio_bus_type, NULL, name);
  1396. if (!found)
  1397. return NULL;
  1398. return to_vio_dev(found);
  1399. }
  1400. /**
  1401. * vio_find_node - find an already-registered vio_dev
  1402. * @vnode: device_node of the virtual device we're looking for
  1403. */
  1404. struct vio_dev *vio_find_node(struct device_node *vnode)
  1405. {
  1406. const uint32_t *unit_address;
  1407. char kobj_name[20];
  1408. struct device_node *vnode_parent;
  1409. const char *dev_type;
  1410. vnode_parent = of_get_parent(vnode);
  1411. if (!vnode_parent)
  1412. return NULL;
  1413. dev_type = of_get_property(vnode_parent, "device_type", NULL);
  1414. of_node_put(vnode_parent);
  1415. if (!dev_type)
  1416. return NULL;
  1417. /* construct the kobject name from the device node */
  1418. if (!strcmp(dev_type, "vdevice")) {
  1419. unit_address = of_get_property(vnode, "reg", NULL);
  1420. if (!unit_address)
  1421. return NULL;
  1422. snprintf(kobj_name, sizeof(kobj_name), "%x", *unit_address);
  1423. } else if (!strcmp(dev_type, "ibm,platform-facilities"))
  1424. snprintf(kobj_name, sizeof(kobj_name), "%s", vnode->name);
  1425. else
  1426. return NULL;
  1427. return vio_find_name(kobj_name);
  1428. }
  1429. EXPORT_SYMBOL(vio_find_node);
  1430. int vio_enable_interrupts(struct vio_dev *dev)
  1431. {
  1432. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
  1433. if (rc != H_SUCCESS)
  1434. printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
  1435. return rc;
  1436. }
  1437. EXPORT_SYMBOL(vio_enable_interrupts);
  1438. int vio_disable_interrupts(struct vio_dev *dev)
  1439. {
  1440. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
  1441. if (rc != H_SUCCESS)
  1442. printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
  1443. return rc;
  1444. }
  1445. EXPORT_SYMBOL(vio_disable_interrupts);
  1446. #endif /* CONFIG_PPC_PSERIES */