vio.c 42 KB

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