vio.c 47 KB

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