18 #include <linux/types.h>
20 #include <linux/stat.h>
21 #include <linux/device.h>
23 #include <linux/slab.h>
25 #include <linux/export.h>
30 #include <asm/iommu.h>
39 static struct vio_dev vio_bus_device = {
42 .dev.init_name =
"vio",
46 #ifdef CONFIG_PPC_SMLPAR
59 #define VIO_CMO_BALANCE_DELAY 100
62 #define VIO_CMO_BALANCE_CHUNK 131072
70 struct vio_cmo_dev_entry {
96 struct vio_cmo_pool excess;
107 static int vio_cmo_num_OF_devs(
void)
128 of_node_put(node_vroot);
146 static inline int vio_cmo_alloc(
struct vio_dev *viodev,
size_t size)
149 size_t reserve_free = 0;
150 size_t excess_free = 0;
156 if (viodev->cmo.entitled > viodev->cmo.allocated)
157 reserve_free = viodev->cmo.entitled - viodev->cmo.allocated;
160 if (vio_cmo.spare >= VIO_CMO_MIN_ENT)
161 excess_free = vio_cmo.excess.free;
164 if ((reserve_free + excess_free) >= size) {
165 vio_cmo.curr +=
size;
166 if (vio_cmo.curr > vio_cmo.high)
167 vio_cmo.high = vio_cmo.curr;
168 viodev->cmo.allocated +=
size;
169 size -=
min(reserve_free, size);
170 vio_cmo.excess.free -=
size;
174 spin_unlock_irqrestore(&vio_cmo.lock, flags);
190 static inline void vio_cmo_dealloc(
struct vio_dev *viodev,
size_t size)
193 size_t spare_needed = 0;
194 size_t excess_freed = 0;
195 size_t reserve_freed =
size;
200 vio_cmo.curr -=
size;
203 if (viodev->cmo.allocated > viodev->cmo.entitled) {
204 excess_freed =
min(reserve_freed, (viodev->cmo.allocated -
205 viodev->cmo.entitled));
206 reserve_freed -= excess_freed;
210 viodev->cmo.allocated -= (reserve_freed + excess_freed);
213 spare_needed = VIO_CMO_MIN_ENT - vio_cmo.spare;
219 if (spare_needed && excess_freed) {
220 tmp =
min(excess_freed, spare_needed);
221 vio_cmo.excess.size -=
tmp;
222 vio_cmo.reserve.size +=
tmp;
223 vio_cmo.spare +=
tmp;
235 if (spare_needed && reserve_freed) {
236 tmp =
min3(spare_needed, reserve_freed, (viodev->cmo.entitled - VIO_CMO_MIN_ENT));
238 vio_cmo.spare +=
tmp;
239 viodev->cmo.entitled -=
tmp;
240 reserve_freed -=
tmp;
250 if (excess_freed && (vio_cmo.desired > vio_cmo.reserve.size)) {
251 tmp =
min(excess_freed, (vio_cmo.desired - vio_cmo.reserve.size));
253 vio_cmo.excess.size -=
tmp;
254 vio_cmo.reserve.size +=
tmp;
261 vio_cmo.excess.free += excess_freed;
265 spin_unlock_irqrestore(&vio_cmo.lock, flags);
282 struct vio_cmo_dev_entry *dev_ent;
289 if (new_entitlement > vio_cmo.entitled) {
290 delta = new_entitlement - vio_cmo.entitled;
293 if (vio_cmo.spare < VIO_CMO_MIN_ENT) {
294 tmp =
min(delta, (VIO_CMO_MIN_ENT - vio_cmo.spare));
295 vio_cmo.spare +=
tmp;
296 vio_cmo.reserve.size +=
tmp;
301 vio_cmo.entitled +=
delta;
302 vio_cmo.excess.size +=
delta;
303 vio_cmo.excess.free +=
delta;
309 delta = vio_cmo.entitled - new_entitlement;
310 avail = vio_cmo.excess.free;
320 viodev = dev_ent->viodev;
321 if ((viodev->cmo.entitled > viodev->cmo.allocated) &&
322 (viodev->cmo.entitled > VIO_CMO_MIN_ENT))
323 avail += viodev->cmo.entitled -
324 max_t(
size_t, viodev->cmo.allocated,
328 if (delta <= avail) {
329 vio_cmo.entitled -=
delta;
332 tmp =
min(vio_cmo.excess.free, delta);
333 vio_cmo.excess.size -=
tmp;
334 vio_cmo.excess.free -=
tmp;
345 viodev = dev_ent->viodev;
347 if ((viodev->cmo.entitled > viodev->cmo.allocated) &&
348 (viodev->cmo.entitled > VIO_CMO_MIN_ENT))
349 tmp = viodev->cmo.entitled -
350 max_t(
size_t, viodev->cmo.allocated,
352 viodev->cmo.entitled -=
min(tmp, delta);
353 delta -=
min(tmp, delta);
356 spin_unlock_irqrestore(&vio_cmo.lock, flags);
362 spin_unlock_irqrestore(&vio_cmo.lock, flags);
391 struct vio_cmo_dev_entry *dev_ent;
394 int devcount = 0, fulfilled;
396 cmo =
container_of(work,
struct vio_cmo, balance_q.work);
401 cmo->min = vio_cmo_num_OF_devs() * VIO_CMO_MIN_ENT;
402 BUG_ON(cmo->min > cmo->entitled);
403 cmo->spare =
min_t(
size_t, VIO_CMO_MIN_ENT, (cmo->entitled - cmo->min));
404 cmo->min += cmo->spare;
405 cmo->desired = cmo->min;
411 avail = cmo->entitled - cmo->spare;
413 viodev = dev_ent->viodev;
415 viodev->cmo.entitled = VIO_CMO_MIN_ENT;
416 cmo->desired += (viodev->cmo.desired - VIO_CMO_MIN_ENT);
417 avail -=
max_t(
size_t, viodev->cmo.allocated, VIO_CMO_MIN_ENT);
425 level = VIO_CMO_MIN_ENT;
429 viodev = dev_ent->viodev;
431 if (viodev->cmo.desired <=
level) {
441 chunk =
min_t(
size_t, avail, VIO_CMO_BALANCE_CHUNK);
442 chunk =
min(chunk, (viodev->cmo.desired -
443 viodev->cmo.entitled));
444 viodev->cmo.entitled +=
chunk;
451 need =
max(viodev->cmo.allocated, viodev->cmo.entitled)-
456 if (fulfilled == devcount)
458 level += VIO_CMO_BALANCE_CHUNK;
462 cmo->reserve.size = cmo->min;
463 cmo->excess.free = 0;
464 cmo->excess.size = 0;
467 viodev = dev_ent->viodev;
469 if (viodev->cmo.entitled)
470 cmo->reserve.size += (viodev->cmo.entitled -
473 if (viodev->cmo.allocated > viodev->cmo.entitled)
474 need += viodev->cmo.allocated - viodev->cmo.entitled;
476 cmo->excess.size = cmo->entitled - cmo->reserve.size;
477 cmo->excess.free = cmo->excess.size - need;
480 spin_unlock_irqrestore(&vio_cmo.lock, flags);
483 static void *vio_dma_iommu_alloc_coherent(
struct device *
dev,
size_t size,
487 struct vio_dev *viodev = to_vio_dev(dev);
495 ret =
dma_iommu_ops.alloc(dev, size, dma_handle, flag, attrs);
504 static void vio_dma_iommu_free_coherent(
struct device *dev,
size_t size,
508 struct vio_dev *viodev = to_vio_dev(dev);
516 unsigned long offset,
size_t size,
520 struct vio_dev *viodev = to_vio_dev(dev);
523 if (vio_cmo_alloc(viodev,
roundup(size, IOMMU_PAGE_SIZE))) {
528 ret =
dma_iommu_ops.map_page(dev, page, offset, size, direction, attrs);
530 vio_cmo_dealloc(viodev,
roundup(size, IOMMU_PAGE_SIZE));
537 static void vio_dma_iommu_unmap_page(
struct device *dev,
dma_addr_t dma_handle,
542 struct vio_dev *viodev = to_vio_dev(dev);
544 dma_iommu_ops.unmap_page(dev, dma_handle, size, direction, attrs);
546 vio_cmo_dealloc(viodev,
roundup(size, IOMMU_PAGE_SIZE));
553 struct vio_dev *viodev = to_vio_dev(dev);
556 size_t alloc_size = 0;
558 for (sgl = sglist; count < nelems; count++, sgl++)
561 if (vio_cmo_alloc(viodev, alloc_size)) {
566 ret =
dma_iommu_ops.map_sg(dev, sglist, nelems, direction, attrs);
569 vio_cmo_dealloc(viodev, alloc_size);
574 for (sgl = sglist, count = 0; count <
ret; count++, sgl++)
575 alloc_size -=
roundup(sgl->dma_length, IOMMU_PAGE_SIZE);
577 vio_cmo_dealloc(viodev, alloc_size);
582 static void vio_dma_iommu_unmap_sg(
struct device *dev,
587 struct vio_dev *viodev = to_vio_dev(dev);
589 size_t alloc_size = 0;
592 for (sgl = sglist; count < nelems; count++, sgl++)
593 alloc_size +=
roundup(sgl->dma_length, IOMMU_PAGE_SIZE);
595 dma_iommu_ops.unmap_sg(dev, sglist, nelems, direction, attrs);
597 vio_cmo_dealloc(viodev, alloc_size);
600 static int vio_dma_iommu_dma_supported(
struct device *dev,
u64 mask)
605 static u64 vio_dma_get_required_mask(
struct device *dev)
611 .
alloc = vio_dma_iommu_alloc_coherent,
612 .free = vio_dma_iommu_free_coherent,
614 .map_sg = vio_dma_iommu_map_sg,
615 .unmap_sg = vio_dma_iommu_unmap_sg,
616 .map_page = vio_dma_iommu_map_page,
617 .unmap_page = vio_dma_iommu_unmap_page,
618 .dma_supported = vio_dma_iommu_dma_supported,
619 .get_required_mask = vio_dma_get_required_mask,
635 struct vio_cmo_dev_entry *dev_ent;
638 if (!firmware_has_feature(FW_FEATURE_CMO))
642 if (desired < VIO_CMO_MIN_ENT)
643 desired = VIO_CMO_MIN_ENT;
651 if (viodev == dev_ent->viodev) {
656 spin_unlock_irqrestore(&vio_cmo.lock, flags);
661 if (desired >= viodev->cmo.desired) {
663 vio_cmo.desired += desired - viodev->cmo.desired;
664 viodev->cmo.desired = desired;
667 vio_cmo.desired -= viodev->cmo.desired - desired;
668 viodev->cmo.desired = desired;
673 if (viodev->cmo.entitled > desired) {
674 vio_cmo.reserve.size -= viodev->cmo.entitled - desired;
675 vio_cmo.excess.size += viodev->cmo.entitled - desired;
681 if (viodev->cmo.allocated < viodev->cmo.entitled)
682 vio_cmo.excess.free += viodev->cmo.entitled -
683 max(viodev->cmo.allocated, desired);
684 viodev->cmo.entitled = desired;
688 spin_unlock_irqrestore(&vio_cmo.lock, flags);
705 static int vio_cmo_bus_probe(
struct vio_dev *viodev)
707 struct vio_cmo_dev_entry *dev_ent;
715 switch (viodev->family) {
718 "ibm,my-dma-window",
NULL))
725 dev_warn(dev,
"unknown device family: %d\n", viodev->family);
733 if (!viodrv->get_desired_dma) {
734 dev_err(dev,
"%s: device driver does not support CMO\n",
739 viodev->cmo.desired = IOMMU_PAGE_ALIGN(viodrv->get_desired_dma(viodev));
740 if (viodev->cmo.desired < VIO_CMO_MIN_ENT)
741 viodev->cmo.desired = VIO_CMO_MIN_ENT;
742 size = VIO_CMO_MIN_ENT;
744 dev_ent =
kmalloc(
sizeof(
struct vio_cmo_dev_entry),
749 dev_ent->viodev = viodev;
751 list_add(&dev_ent->list, &vio_cmo.device_list);
753 viodev->cmo.desired = 0;
764 if (vio_cmo.min == ((vio_cmo_num_OF_devs() + 1) *
768 vio_cmo.desired += (viodev->cmo.desired -
773 tmp = vio_cmo.spare + vio_cmo.excess.free;
775 dev_err(dev,
"%s: insufficient free "
776 "entitlement to add device. "
777 "Need %lu, have %lu\n", __func__,
778 size, (vio_cmo.spare + tmp));
779 spin_unlock_irqrestore(&vio_cmo.lock, flags);
784 tmp =
min(size, vio_cmo.excess.free);
785 vio_cmo.excess.free -=
tmp;
786 vio_cmo.excess.size -=
tmp;
787 vio_cmo.reserve.size +=
tmp;
790 vio_cmo.spare -= size -
tmp;
794 vio_cmo.desired += viodev->cmo.desired;
796 spin_unlock_irqrestore(&vio_cmo.lock, flags);
810 static void vio_cmo_bus_remove(
struct vio_dev *viodev)
812 struct vio_cmo_dev_entry *dev_ent;
817 if (viodev->cmo.allocated) {
818 dev_err(&viodev->
dev,
"%s: device had %lu bytes of IO "
819 "allocated after remove operation.\n",
820 __func__, viodev->cmo.allocated);
829 if (viodev == dev_ent->viodev) {
840 if (viodev->cmo.entitled) {
846 vio_cmo.desired -= (viodev->cmo.desired - VIO_CMO_MIN_ENT);
853 viodev->cmo.entitled -= VIO_CMO_MIN_ENT;
856 if (viodev->cmo.entitled && (vio_cmo.spare < VIO_CMO_MIN_ENT)) {
857 tmp =
min(viodev->cmo.entitled, (VIO_CMO_MIN_ENT -
859 vio_cmo.spare +=
tmp;
860 viodev->cmo.entitled -=
tmp;
864 vio_cmo.excess.size += viodev->cmo.entitled;
865 vio_cmo.excess.free += viodev->cmo.entitled;
866 vio_cmo.reserve.size -= viodev->cmo.entitled;
873 viodev->cmo.entitled = VIO_CMO_MIN_ENT;
874 viodev->cmo.desired = VIO_CMO_MIN_ENT;
878 spin_unlock_irqrestore(&vio_cmo.lock, flags);
881 static void vio_cmo_set_dma_ops(
struct vio_dev *viodev)
883 set_dma_ops(&viodev->
dev, &vio_dma_mapping_ops);
893 static void vio_cmo_bus_init(
void)
895 struct hvcall_mpp_data mpp_data;
898 memset(&vio_cmo, 0,
sizeof(
struct vio_cmo));
900 INIT_LIST_HEAD(&vio_cmo.device_list);
910 if (err != H_SUCCESS) {
912 "entitlement. (%d)\n", __func__, err);
913 vio_cmo.entitled = 0;
915 vio_cmo.entitled = mpp_data.entitled_mem;
919 vio_cmo.spare = VIO_CMO_MIN_ENT;
920 vio_cmo.reserve.size = vio_cmo.spare;
921 vio_cmo.reserve.size += (vio_cmo_num_OF_devs() *
923 if (vio_cmo.reserve.size > vio_cmo.entitled) {
926 panic(
"%s: Insufficient system entitlement", __func__);
930 vio_cmo.excess.size = vio_cmo.entitled - vio_cmo.reserve.size;
931 vio_cmo.excess.free = vio_cmo.excess.size;
932 vio_cmo.min = vio_cmo.reserve.size;
933 vio_cmo.desired = vio_cmo.reserve.size;
938 #define viodev_cmo_rd_attr(name) \
939 static ssize_t viodev_cmo_##name##_show(struct device *dev, \
940 struct device_attribute *attr, \
943 return sprintf(buf, "%lu\n", to_vio_dev(dev)->cmo.name); \
946 static ssize_t viodev_cmo_allocs_failed_show(
struct device *dev,
949 struct vio_dev *viodev = to_vio_dev(dev);
953 static ssize_t viodev_cmo_allocs_failed_reset(
struct device *dev,
956 struct vio_dev *viodev = to_vio_dev(dev);
964 struct vio_dev *viodev = to_vio_dev(dev);
976 viodev_cmo_rd_attr(desired);
977 viodev_cmo_rd_attr(entitled);
978 viodev_cmo_rd_attr(allocated);
989 viodev_cmo_desired_show, viodev_cmo_desired_set),
993 viodev_cmo_allocs_failed_show, viodev_cmo_allocs_failed_reset),
999 #define viobus_cmo_rd_attr(name) \
1001 viobus_cmo_##name##_show(struct bus_type *bt, char *buf) \
1003 return sprintf(buf, "%lu\n", vio_cmo.name); \
1006 #define viobus_cmo_pool_rd_attr(name, var) \
1008 viobus_cmo_##name##_pool_show_##var(struct bus_type *bt, char *buf) \
1010 return sprintf(buf, "%lu\n", vio_cmo.name.var); \
1016 unsigned long flags;
1019 vio_cmo.high = vio_cmo.curr;
1020 spin_unlock_irqrestore(&vio_cmo.lock, flags);
1025 viobus_cmo_rd_attr(entitled);
1026 viobus_cmo_pool_rd_attr(
reserve, size);
1027 viobus_cmo_pool_rd_attr(excess, size);
1028 viobus_cmo_pool_rd_attr(excess,
free);
1029 viobus_cmo_rd_attr(spare);
1030 viobus_cmo_rd_attr(
min);
1031 viobus_cmo_rd_attr(desired);
1032 viobus_cmo_rd_attr(
curr);
1033 viobus_cmo_rd_attr(
high);
1045 viobus_cmo_high_show, viobus_cmo_high_reset),
1049 static void vio_cmo_sysfs_init(
void)
1057 static int vio_cmo_bus_probe(
struct vio_dev *viodev) {
return 0; }
1058 static void vio_cmo_bus_remove(
struct vio_dev *viodev) {}
1059 static void vio_cmo_set_dma_ops(
struct vio_dev *viodev) {}
1060 static void vio_cmo_bus_init(
void) {}
1061 static void vio_cmo_sysfs_init(
void) { }
1099 unsigned long deadline = 0;
1107 hret = plpar_hcall_norets(H_COP, op->flags,
1109 op->in, op->inlen, op->out,
1110 op->outlen, op->csbcpb);
1112 if (hret == H_SUCCESS ||
1113 (hret != H_NOT_ENOUGH_RESOURCES &&
1114 hret != H_BUSY && hret != H_RESOURCE) ||
1118 dev_dbg(dev,
"%s: hcall ret(%ld), retrying.\n", __func__, hret);
1135 case H_NOT_ENOUGH_RESOURCES:
1146 dev_dbg(dev,
"%s: Sync h_cop_op failure (ret:%d) (hret:%ld)\n",
1147 __func__, ret, hret);
1149 op->hcall_err = hret;
1156 const unsigned char *dma_window;
1161 "ibm,my-dma-window",
NULL);
1170 &tbl->it_index, &offset, &size);
1177 tbl->it_type = TCE_VB;
1178 tbl->it_blocksize = 16;
1196 while (ids->
type[0] !=
'\0') {
1211 static int vio_bus_probe(
struct device *dev)
1213 struct vio_dev *viodev = to_vio_dev(dev);
1221 id = vio_match_device(viodrv->
id_table, viodev);
1223 memset(&viodev->cmo, 0,
sizeof(viodev->cmo));
1224 if (firmware_has_feature(FW_FEATURE_CMO)) {
1225 error = vio_cmo_bus_probe(viodev);
1229 error = viodrv->
probe(viodev,
id);
1230 if (error && firmware_has_feature(FW_FEATURE_CMO))
1231 vio_cmo_bus_remove(viodev);
1238 static int vio_bus_remove(
struct device *dev)
1240 struct vio_dev *viodev = to_vio_dev(dev);
1252 ret = viodrv->
remove(viodev);
1254 if (!ret && firmware_has_feature(FW_FEATURE_CMO))
1255 vio_cmo_bus_remove(viodev);
1266 const char *mod_name)
1268 pr_debug(
"%s: driver %s registering\n", __func__, viodrv->
name);
1272 viodrv->
driver.pm = viodrv->pm;
1275 viodrv->
driver.mod_name = mod_name;
1294 struct iommu_table *tbl = get_iommu_table_base(dev);
1299 kfree(to_vio_dev(dev));
1316 const unsigned int *pfo_resid =
NULL;
1317 enum vio_dev_family
family;
1318 const char *of_node_name = of_node->
name ? of_node->
name :
"<unknown>";
1331 pr_warn(
"%s: parent(%s) of %s not recognized.\n",
1335 of_node_put(parent_node);
1338 of_node_put(parent_node);
1340 pr_warn(
"%s: could not determine the parent of node %s.\n",
1341 __func__, of_node_name);
1345 if (family == PFO) {
1347 pr_debug(
"%s: Skipping the interrupt controller %s.\n",
1348 __func__, of_node_name);
1355 if (viodev ==
NULL) {
1356 pr_warn(
"%s: allocation failure for VIO device.\n", __func__);
1362 if (viodev->family == VDEVICE) {
1366 pr_warn(
"%s: node %s is missing the 'device_type' "
1367 "property.\n", __func__, of_node_name);
1372 if (unit_address ==
NULL) {
1373 pr_warn(
"%s: node %s missing 'reg'\n",
1374 __func__, of_node_name);
1385 if (pfo_resid !=
NULL)
1386 viodev->resource_id = *pfo_resid;
1388 unit_address =
NULL;
1390 viodev->
type = of_node_name;
1394 viodev->name = of_node->
name;
1395 viodev->
dev.of_node = of_node_get(of_node);
1400 viodev->
dev.parent = &vio_bus_device.
dev;
1402 viodev->
dev.release = vio_dev_release;
1405 if (firmware_has_feature(FW_FEATURE_CMO))
1406 vio_cmo_set_dma_ops(viodev);
1410 set_iommu_table_base(&viodev->
dev,
1411 vio_build_iommu_table(viodev));
1422 __func__, dev_name(&viodev->
dev));
1444 static void vio_bus_scan_register_devices(
char *root_name)
1459 while (node_child) {
1463 of_node_put(node_root);
1470 static int __init vio_bus_init(
void)
1474 if (firmware_has_feature(FW_FEATURE_CMO))
1475 vio_cmo_sysfs_init();
1494 if (firmware_has_feature(FW_FEATURE_CMO))
1501 static int __init vio_device_init(
void)
1503 vio_bus_scan_register_devices(
"vdevice");
1504 vio_bus_scan_register_devices(
"ibm,platform-facilities");
1513 return sprintf(buf,
"%s\n", to_vio_dev(dev)->
name);
1521 return sprintf(buf,
"%s\n", of_node_full_name(of_node));
1538 return sprintf(buf,
"vio:T%sS%s\n", vio_dev->
type, cp);
1556 const struct vio_dev *vio_dev = to_vio_dev(dev);
1557 struct vio_driver *vio_drv = to_vio_driver(drv);
1560 return (ids !=
NULL) && (vio_match_device(ids, vio_dev) !=
NULL);
1565 const struct vio_dev *vio_dev = to_vio_dev(dev);
1582 .dev_attrs = vio_dev_attrs,
1583 .uevent = vio_hotplug,
1584 .match = vio_bus_match,
1585 .probe = vio_bus_probe,
1586 .remove = vio_bus_remove,
1604 #ifdef CONFIG_PPC_PSERIES
1608 static struct vio_dev *vio_find_name(
const char *
name)
1616 return to_vio_dev(found);
1623 struct vio_dev *vio_find_node(
struct device_node *vnode)
1635 of_node_put(vnode_parent);
1640 if (!
strcmp(dev_type,
"vdevice")) {
1644 snprintf(kobj_name,
sizeof(kobj_name),
"%x", *unit_address);
1645 }
else if (!
strcmp(dev_type,
"ibm,platform-facilities"))
1646 snprintf(kobj_name,
sizeof(kobj_name),
"%s", vnode->
name);
1650 return vio_find_name(kobj_name);
1654 int vio_enable_interrupts(
struct vio_dev *dev)
1656 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
1657 if (rc != H_SUCCESS)
1663 int vio_disable_interrupts(
struct vio_dev *dev)
1665 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
1666 if (rc != H_SUCCESS)