Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

s390/cio: introduce DMA pools to cio

To support protected virtualization cio will need to make sure the
memory used for communication with the hypervisor is DMA memory.

Let us introduce one global pool for cio.

Our DMA pools are implemented as a gen_pool backed with DMA pages. The
idea is to avoid each allocation effectively wasting a page, as we
typically allocate much less than PAGE_SIZE.

Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
Reviewed-by: Sebastian Ott <sebott@linux.ibm.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Michael Mueller <mimu@linux.ibm.com>
Tested-by: Michael Mueller <mimu@linux.ibm.com>
Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>

authored by

Halil Pasic and committed by
Heiko Carstens
bb99332a 64e1f0c5

+141 -4
+1
arch/s390/Kconfig
··· 190 190 select ARCH_HAS_SCALED_CPUTIME 191 191 select HAVE_NMI 192 192 select SWIOTLB 193 + select GENERIC_ALLOCATOR 193 194 194 195 195 196 config SCHED_OMIT_FRAME_POINTER
+11
arch/s390/include/asm/cio.h
··· 7 7 8 8 #include <linux/spinlock.h> 9 9 #include <linux/bitops.h> 10 + #include <linux/genalloc.h> 10 11 #include <asm/types.h> 11 12 12 13 #define LPM_ANYPATH 0xff ··· 328 327 329 328 void channel_subsystem_reinit(void); 330 329 extern void css_schedule_reprobe(void); 330 + 331 + extern void *cio_dma_zalloc(size_t size); 332 + extern void cio_dma_free(void *cpu_addr, size_t size); 333 + extern struct device *cio_get_dma_css_dev(void); 334 + 335 + void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev, 336 + size_t size); 337 + void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size); 338 + void cio_gp_dma_destroy(struct gen_pool *gp_dma, struct device *dma_dev); 339 + struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages); 331 340 332 341 /* Function from drivers/s390/cio/chsc.c */ 333 342 int chsc_sstpc(void *page, unsigned int op, u16 ctrl, u64 *clock_delta);
+129 -4
drivers/s390/cio/css.c
··· 20 20 #include <linux/reboot.h> 21 21 #include <linux/suspend.h> 22 22 #include <linux/proc_fs.h> 23 + #include <linux/genalloc.h> 24 + #include <linux/dma-mapping.h> 23 25 #include <asm/isc.h> 24 26 #include <asm/crw.h> 25 27 ··· 226 224 INIT_WORK(&sch->todo_work, css_sch_todo); 227 225 sch->dev.release = &css_subchannel_release; 228 226 device_initialize(&sch->dev); 227 + /* 228 + * The physical addresses of some the dma structures that can 229 + * belong to a subchannel need to fit 31 bit width (e.g. ccw). 230 + */ 231 + sch->dev.coherent_dma_mask = DMA_BIT_MASK(31); 232 + sch->dev.dma_mask = &sch->dev.coherent_dma_mask; 229 233 return sch; 230 234 231 235 err: ··· 907 899 dev_set_name(&css->device, "css%x", nr); 908 900 css->device.groups = cssdev_attr_groups; 909 901 css->device.release = channel_subsystem_release; 902 + /* 903 + * We currently allocate notifier bits with this (using 904 + * css->device as the device argument with the DMA API) 905 + * and are fine with 64 bit addresses. 906 + */ 907 + css->device.coherent_dma_mask = DMA_BIT_MASK(64); 908 + css->device.dma_mask = &css->device.coherent_dma_mask; 910 909 911 910 mutex_init(&css->mutex); 912 911 css->cssid = chsc_get_cssid(nr); ··· 1033 1018 .notifier_call = css_power_event, 1034 1019 }; 1035 1020 1021 + #define CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO) 1022 + static struct gen_pool *cio_dma_pool; 1023 + 1024 + /* Currently cio supports only a single css */ 1025 + struct device *cio_get_dma_css_dev(void) 1026 + { 1027 + return &channel_subsystems[0]->device; 1028 + } 1029 + 1030 + struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages) 1031 + { 1032 + struct gen_pool *gp_dma; 1033 + void *cpu_addr; 1034 + dma_addr_t dma_addr; 1035 + int i; 1036 + 1037 + gp_dma = gen_pool_create(3, -1); 1038 + if (!gp_dma) 1039 + return NULL; 1040 + for (i = 0; i < nr_pages; ++i) { 1041 + cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr, 1042 + CIO_DMA_GFP); 1043 + if (!cpu_addr) 1044 + return gp_dma; 1045 + gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr, 1046 + dma_addr, PAGE_SIZE, -1); 1047 + } 1048 + return gp_dma; 1049 + } 1050 + 1051 + static void __gp_dma_free_dma(struct gen_pool *pool, 1052 + struct gen_pool_chunk *chunk, void *data) 1053 + { 1054 + size_t chunk_size = chunk->end_addr - chunk->start_addr + 1; 1055 + 1056 + dma_free_coherent((struct device *) data, chunk_size, 1057 + (void *) chunk->start_addr, 1058 + (dma_addr_t) chunk->phys_addr); 1059 + } 1060 + 1061 + void cio_gp_dma_destroy(struct gen_pool *gp_dma, struct device *dma_dev) 1062 + { 1063 + if (!gp_dma) 1064 + return; 1065 + /* this is quite ugly but no better idea */ 1066 + gen_pool_for_each_chunk(gp_dma, __gp_dma_free_dma, dma_dev); 1067 + gen_pool_destroy(gp_dma); 1068 + } 1069 + 1070 + static int cio_dma_pool_init(void) 1071 + { 1072 + /* No need to free up the resources: compiled in */ 1073 + cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1); 1074 + if (!cio_dma_pool) 1075 + return -ENOMEM; 1076 + return 0; 1077 + } 1078 + 1079 + void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev, 1080 + size_t size) 1081 + { 1082 + dma_addr_t dma_addr; 1083 + unsigned long addr; 1084 + size_t chunk_size; 1085 + 1086 + if (!gp_dma) 1087 + return NULL; 1088 + addr = gen_pool_alloc(gp_dma, size); 1089 + while (!addr) { 1090 + chunk_size = round_up(size, PAGE_SIZE); 1091 + addr = (unsigned long) dma_alloc_coherent(dma_dev, 1092 + chunk_size, &dma_addr, CIO_DMA_GFP); 1093 + if (!addr) 1094 + return NULL; 1095 + gen_pool_add_virt(gp_dma, addr, dma_addr, chunk_size, -1); 1096 + addr = gen_pool_alloc(gp_dma, size); 1097 + } 1098 + return (void *) addr; 1099 + } 1100 + 1101 + void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size) 1102 + { 1103 + if (!cpu_addr) 1104 + return; 1105 + memset(cpu_addr, 0, size); 1106 + gen_pool_free(gp_dma, (unsigned long) cpu_addr, size); 1107 + } 1108 + 1109 + /* 1110 + * Allocate dma memory from the css global pool. Intended for memory not 1111 + * specific to any single device within the css. The allocated memory 1112 + * is not guaranteed to be 31-bit addressable. 1113 + * 1114 + * Caution: Not suitable for early stuff like console. 1115 + */ 1116 + void *cio_dma_zalloc(size_t size) 1117 + { 1118 + return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size); 1119 + } 1120 + 1121 + void cio_dma_free(void *cpu_addr, size_t size) 1122 + { 1123 + cio_gp_dma_free(cio_dma_pool, cpu_addr, size); 1124 + } 1125 + 1036 1126 /* 1037 1127 * Now that the driver core is running, we can setup our channel subsystem. 1038 1128 * The struct subchannel's are created during probing. ··· 1179 1059 if (ret) 1180 1060 goto out_unregister; 1181 1061 ret = register_pm_notifier(&css_power_notifier); 1182 - if (ret) { 1183 - unregister_reboot_notifier(&css_reboot_notifier); 1184 - goto out_unregister; 1185 - } 1062 + if (ret) 1063 + goto out_unregister_rn; 1064 + ret = cio_dma_pool_init(); 1065 + if (ret) 1066 + goto out_unregister_pmn; 1186 1067 css_init_done = 1; 1187 1068 1188 1069 /* Enable default isc for I/O subchannels. */ 1189 1070 isc_register(IO_SCH_ISC); 1190 1071 1191 1072 return 0; 1073 + out_unregister_pmn: 1074 + unregister_pm_notifier(&css_power_notifier); 1075 + out_unregister_rn: 1076 + unregister_reboot_notifier(&css_reboot_notifier); 1192 1077 out_unregister: 1193 1078 while (i-- > 0) { 1194 1079 struct channel_subsystem *css = channel_subsystems[i];