vio.c 43 KB

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