1 From d5c6191cc94b358de183cc8c88a5722a79445202 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
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
10 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
12 .../interface/vchiq_arm/vchiq_2835_arm.c | 90 ++++++++++++++-----
13 .../interface/vchiq_arm/vchiq_arm.c | 6 ++
14 .../interface/vchiq_arm/vchiq_arm.h | 1 +
15 3 files changed, 75 insertions(+), 22 deletions(-)
17 --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
18 +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
20 #include <soc/bcm2835/raspberrypi-firmware.h>
22 #define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
23 +#define VC_SAFE(x) (g_use_36bit_addrs ? ((u32)(x) | 0xc0000000) : (u32)(x))
24 +#define IS_VC_SAFE(x) (g_use_36bit_addrs ? !((x) & ~0x3fffffffull) : 1)
26 #include "vchiq_arm.h"
27 #include "vchiq_connected.h"
28 @@ -96,6 +98,7 @@ static void __iomem *g_regs;
30 static unsigned int g_cache_line_size;
31 static struct dma_pool *g_dma_pool;
32 +static unsigned int g_use_36bit_addrs = 0;
33 static unsigned int g_fragments_size;
34 static char *g_fragments_base;
35 static char *g_free_fragments;
36 @@ -139,6 +142,8 @@ int vchiq_platform_init(struct platform_
37 g_cache_line_size = drvdata->cache_line_size;
38 g_fragments_size = 2 * g_cache_line_size;
40 + g_use_36bit_addrs = (dev->dma_pfn_offset == 0);
42 /* Allocate space for the channels in coherent memory */
43 slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
44 frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
45 @@ -150,14 +155,21 @@ int vchiq_platform_init(struct platform_
49 + if (!IS_VC_SAFE(slot_phys)) {
50 + dev_err(dev, "allocated DMA memory %pad is not VC-safe\n",
55 WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
56 + channelbase = VC_SAFE(slot_phys);
58 vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
62 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
63 - (int)slot_phys + slot_mem_size;
64 + channelbase + slot_mem_size;
65 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
68 @@ -193,7 +205,6 @@ int vchiq_platform_init(struct platform_
71 /* Send the base address of the slots to VideoCore */
72 - channelbase = slot_phys;
73 err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
74 &channelbase, sizeof(channelbase));
75 if (err || channelbase) {
76 @@ -282,7 +293,7 @@ vchiq_prepare_bulk_data(VCHIQ_BULK_T *bu
79 bulk->handle = memhandle;
80 - bulk->data = (void *)(unsigned long)pagelistinfo->dma_addr;
81 + bulk->data = (void *)VC_SAFE(pagelistinfo->dma_addr);
84 * Store the pagelistinfo address in remote_data,
85 @@ -570,25 +581,60 @@ create_pagelist(char __user *buf, size_t
87 /* Combine adjacent blocks for performance */
89 - for_each_sg(scatterlist, sg, dma_buffers, i) {
90 - u32 len = sg_dma_len(sg);
91 - u32 addr = sg_dma_address(sg);
93 - /* Note: addrs is the address + page_count - 1
94 - * The firmware expects blocks after the first to be page-
95 - * aligned and a multiple of the page size
98 - WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
99 - WARN_ON(i && (addr & ~PAGE_MASK));
101 - ((addrs[k - 1] & PAGE_MASK) +
102 - (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
103 - == (addr & PAGE_MASK))
104 - addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
106 - addrs[k++] = (addr & PAGE_MASK) |
107 - (((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
108 + if (g_use_36bit_addrs) {
109 + for_each_sg(scatterlist, sg, dma_buffers, i) {
110 + u32 len = sg_dma_len(sg);
111 + u64 addr = sg_dma_address(sg);
112 + u32 page_id = (u32)((addr >> 4) & ~0xff);
113 + u32 sg_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
115 + /* Note: addrs is the address + page_count - 1
116 + * The firmware expects blocks after the first to be page-
117 + * aligned and a multiple of the page size
121 + (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
122 + WARN_ON(i && (addr & ~PAGE_MASK));
123 + WARN_ON(upper_32_bits(addr) > 0xf);
125 + ((addrs[k - 1] & ~0xff) +
126 + (((addrs[k - 1] & 0xff) + 1) << 8)
128 + u32 inc_pages = min(sg_pages,
129 + 0xff - (addrs[k - 1] & 0xff));
130 + addrs[k - 1] += inc_pages;
131 + page_id += inc_pages << 8;
132 + sg_pages -= inc_pages;
135 + u32 inc_pages = min(sg_pages, 0x100u);
136 + addrs[k++] = page_id | (inc_pages - 1);
137 + page_id += inc_pages << 8;
138 + sg_pages -= inc_pages;
142 + for_each_sg(scatterlist, sg, dma_buffers, i) {
143 + u32 len = sg_dma_len(sg);
144 + u32 addr = VC_SAFE(sg_dma_address(sg));
145 + u32 new_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
147 + /* Note: addrs is the address + page_count - 1
148 + * The firmware expects blocks after the first to be page-
149 + * aligned and a multiple of the page size
152 + WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
153 + WARN_ON(i && (addr & ~PAGE_MASK));
155 + ((addrs[k - 1] & PAGE_MASK) +
156 + (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
157 + == (addr & PAGE_MASK))
158 + addrs[k - 1] += new_pages;
160 + addrs[k++] = (addr & PAGE_MASK) | (new_pages - 1);
164 /* Partial cache lines (fragments) require special measures */
165 --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
166 +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
167 @@ -181,6 +181,11 @@ static struct vchiq_drvdata bcm2836_drvd
168 .cache_line_size = 64,
171 +static struct vchiq_drvdata bcm2838_drvdata = {
172 + .cache_line_size = 64,
173 + .use_36bit_addrs = true,
176 static const char *const ioctl_names[] = {
179 @@ -3618,6 +3623,7 @@ vchiq_register_child(struct platform_dev
180 static const struct of_device_id vchiq_of_match[] = {
181 { .compatible = "brcm,bcm2835-vchiq", .data = &bcm2835_drvdata },
182 { .compatible = "brcm,bcm2836-vchiq", .data = &bcm2836_drvdata },
183 + { .compatible = "brcm,bcm2838-vchiq", .data = &bcm2838_drvdata },
186 MODULE_DEVICE_TABLE(of, vchiq_of_match);
187 --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
188 +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
189 @@ -125,6 +125,7 @@ typedef struct vchiq_arm_state_struct {
191 struct vchiq_drvdata {
192 const unsigned int cache_line_size;
193 + const bool use_36bit_addrs;
194 struct rpi_firmware *fw;