bfe6fc3869fc044872ade9c45fbdefbac8571384
[openwrt/staging/chunkeey.git] / target / linux / bcm27xx / patches-5.10 / 950-0128-vchiq-Add-36-bit-address-support.patch
1 From 4237f040ec6d54ef0b3782415d354e5b9a023873 Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.org>
3 Date: Thu, 1 Nov 2018 17:31:37 +0000
4 Subject: [PATCH] vchiq: Add 36-bit address support
5
6 Conditional on a new compatible string, change the pagelist encoding
7 such that the top 24 bits are the pfn, leaving 8 bits for run length
8 (-1).
9
10 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
11
12 staging/vchiq_arm: Fix bcm2711 compatible string
13
14 Fixes: "vchiq: Add 36-bit address support"
15
16 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
17 ---
18 .../interface/vchiq_arm/vchiq_2835_arm.c | 90 ++++++++++++++-----
19 .../interface/vchiq_arm/vchiq_arm.c | 6 ++
20 .../interface/vchiq_arm/vchiq_arm.h | 1 +
21 3 files changed, 75 insertions(+), 22 deletions(-)
22
23 --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
24 +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
25 @@ -16,6 +16,8 @@
26 #include <soc/bcm2835/raspberrypi-firmware.h>
27
28 #define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
29 +#define VC_SAFE(x) (g_use_36bit_addrs ? ((u32)(x) | 0xc0000000) : (u32)(x))
30 +#define IS_VC_SAFE(x) (g_use_36bit_addrs ? !((x) & ~0x3fffffffull) : 1)
31
32 #include "vchiq_arm.h"
33 #include "vchiq_connected.h"
34 @@ -58,6 +60,7 @@ static void __iomem *g_regs;
35 * of 32.
36 */
37 static unsigned int g_cache_line_size = 32;
38 +static unsigned int g_use_36bit_addrs = 0;
39 static unsigned int g_fragments_size;
40 static char *g_fragments_base;
41 static char *g_free_fragments;
42 @@ -100,6 +103,8 @@ int vchiq_platform_init(struct platform_
43 g_cache_line_size = drvdata->cache_line_size;
44 g_fragments_size = 2 * g_cache_line_size;
45
46 + g_use_36bit_addrs = (dev->dma_pfn_offset == 0);
47 +
48 /* Allocate space for the channels in coherent memory */
49 slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
50 frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
51 @@ -111,14 +116,21 @@ int vchiq_platform_init(struct platform_
52 return -ENOMEM;
53 }
54
55 + if (!IS_VC_SAFE(slot_phys)) {
56 + dev_err(dev, "allocated DMA memory %pad is not VC-safe\n",
57 + &slot_phys);
58 + return -ENOMEM;
59 + }
60 +
61 WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
62 + channelbase = VC_SAFE(slot_phys);
63
64 vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
65 if (!vchiq_slot_zero)
66 return -EINVAL;
67
68 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
69 - (int)slot_phys + slot_mem_size;
70 + channelbase + slot_mem_size;
71 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
72 MAX_FRAGMENTS;
73
74 @@ -151,7 +163,6 @@ int vchiq_platform_init(struct platform_
75 }
76
77 /* Send the base address of the slots to VideoCore */
78 - channelbase = slot_phys;
79 err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
80 &channelbase, sizeof(channelbase));
81 if (err || channelbase) {
82 @@ -229,7 +240,7 @@ vchiq_prepare_bulk_data(struct vchiq_bul
83 if (!pagelistinfo)
84 return VCHIQ_ERROR;
85
86 - bulk->data = pagelistinfo->dma_addr;
87 + bulk->data = (void *)VC_SAFE(pagelistinfo->dma_addr);
88
89 /*
90 * Store the pagelistinfo address in remote_data,
91 @@ -447,25 +458,60 @@ create_pagelist(char *buf, char __user *
92
93 /* Combine adjacent blocks for performance */
94 k = 0;
95 - for_each_sg(scatterlist, sg, dma_buffers, i) {
96 - u32 len = sg_dma_len(sg);
97 - u32 addr = sg_dma_address(sg);
98 -
99 - /* Note: addrs is the address + page_count - 1
100 - * The firmware expects blocks after the first to be page-
101 - * aligned and a multiple of the page size
102 - */
103 - WARN_ON(len == 0);
104 - WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
105 - WARN_ON(i && (addr & ~PAGE_MASK));
106 - if (k > 0 &&
107 - ((addrs[k - 1] & PAGE_MASK) +
108 - (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
109 - == (addr & PAGE_MASK))
110 - addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
111 - else
112 - addrs[k++] = (addr & PAGE_MASK) |
113 - (((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
114 + if (g_use_36bit_addrs) {
115 + for_each_sg(scatterlist, sg, dma_buffers, i) {
116 + u32 len = sg_dma_len(sg);
117 + u64 addr = sg_dma_address(sg);
118 + u32 page_id = (u32)((addr >> 4) & ~0xff);
119 + u32 sg_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
120 +
121 + /* Note: addrs is the address + page_count - 1
122 + * The firmware expects blocks after the first to be page-
123 + * aligned and a multiple of the page size
124 + */
125 + WARN_ON(len == 0);
126 + WARN_ON(i &&
127 + (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
128 + WARN_ON(i && (addr & ~PAGE_MASK));
129 + WARN_ON(upper_32_bits(addr) > 0xf);
130 + if (k > 0 &&
131 + ((addrs[k - 1] & ~0xff) +
132 + (((addrs[k - 1] & 0xff) + 1) << 8)
133 + == page_id)) {
134 + u32 inc_pages = min(sg_pages,
135 + 0xff - (addrs[k - 1] & 0xff));
136 + addrs[k - 1] += inc_pages;
137 + page_id += inc_pages << 8;
138 + sg_pages -= inc_pages;
139 + }
140 + while (sg_pages) {
141 + u32 inc_pages = min(sg_pages, 0x100u);
142 + addrs[k++] = page_id | (inc_pages - 1);
143 + page_id += inc_pages << 8;
144 + sg_pages -= inc_pages;
145 + }
146 + }
147 + } else {
148 + for_each_sg(scatterlist, sg, dma_buffers, i) {
149 + u32 len = sg_dma_len(sg);
150 + u32 addr = VC_SAFE(sg_dma_address(sg));
151 + u32 new_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
152 +
153 + /* Note: addrs is the address + page_count - 1
154 + * The firmware expects blocks after the first to be page-
155 + * aligned and a multiple of the page size
156 + */
157 + WARN_ON(len == 0);
158 + WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
159 + WARN_ON(i && (addr & ~PAGE_MASK));
160 + if (k > 0 &&
161 + ((addrs[k - 1] & PAGE_MASK) +
162 + (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
163 + == (addr & PAGE_MASK))
164 + addrs[k - 1] += new_pages;
165 + else
166 + addrs[k++] = (addr & PAGE_MASK) | (new_pages - 1);
167 + }
168 }
169
170 /* Partial cache lines (fragments) require special measures */
171 --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
172 +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
173 @@ -118,6 +118,11 @@ static struct vchiq_drvdata bcm2836_drvd
174 .cache_line_size = 64,
175 };
176
177 +static struct vchiq_drvdata bcm2711_drvdata = {
178 + .cache_line_size = 64,
179 + .use_36bit_addrs = true,
180 +};
181 +
182 static const char *const ioctl_names[] = {
183 "CONNECT",
184 "SHUTDOWN",
185 @@ -2679,6 +2684,7 @@ void vchiq_platform_conn_state_changed(s
186 static const struct of_device_id vchiq_of_match[] = {
187 { .compatible = "brcm,bcm2835-vchiq", .data = &bcm2835_drvdata },
188 { .compatible = "brcm,bcm2836-vchiq", .data = &bcm2836_drvdata },
189 + { .compatible = "brcm,bcm2711-vchiq", .data = &bcm2711_drvdata },
190 {},
191 };
192 MODULE_DEVICE_TABLE(of, vchiq_of_match);
193 --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
194 +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
195 @@ -52,6 +52,7 @@ struct vchiq_arm_state {
196
197 struct vchiq_drvdata {
198 const unsigned int cache_line_size;
199 + const bool use_36bit_addrs;
200 struct rpi_firmware *fw;
201 };
202