vio.c 41 KB

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