Skip to content

Commit a89bfc5

Browse files
committed
tinydrm/mipi-dbi: Use dma-safe buffers for all SPI transfers
Buffers passed to spi_sync() must be dma-safe even for tiny buffers since some SPI controllers use DMA for all transfers. Example splat with CONFIG_DMA_API_DEBUG enabled: [ 23.750467] DMA-API: dw_dmac_pci 0000:00:15.0: device driver maps memory from stack [probable addr=000000001e49185d] [ 23.750529] WARNING: CPU: 1 PID: 1296 at kernel/dma/debug.c:1161 check_for_stack+0xb7/0x190 [ 23.750533] Modules linked in: mmc_block(+) spi_pxa2xx_platform(+) pwm_lpss_pci pwm_lpss spi_pxa2xx_pci sdhci_pci cqhci intel_mrfld_pwrbtn extcon_intel_mrfld sdhci intel_mrfld_adc led_class mmc_core ili9341 mipi_dbi tinydrm backlight ti_ads7950 industrialio_triggered_buffer kfifo_buf intel_soc_pmic_mrfld hci_uart btbcm [ 23.750599] CPU: 1 PID: 1296 Comm: modprobe Not tainted 5.0.0-rc7+ raspberrypi#236 [ 23.750605] Hardware name: Intel Corporation Merrifield/BODEGA BAY, BIOS 542 2015.01.21:18.19.48 [ 23.750620] RIP: 0010:check_for_stack+0xb7/0x190 [ 23.750630] Code: 8b 6d 50 4d 85 ed 75 04 4c 8b 6d 10 48 89 ef e8 2f 8b 44 00 48 89 c6 4a 8d 0c 23 4c 89 ea 48 c7 c7 88 d0 82 b4 e8 40 7c f9 ff <0f> 0b 8b 05 79 00 4b 01 85 c0 74 07 5b 5d 41 5c 41 5d c3 8b 05 54 [ 23.750637] RSP: 0000:ffff97bbc0292fa0 EFLAGS: 00010286 [ 23.750646] RAX: 0000000000000000 RBX: ffff97bbc0290000 RCX: 0000000000000006 [ 23.750652] RDX: 0000000000000007 RSI: 0000000000000002 RDI: ffff94b33e115450 [ 23.750658] RBP: ffff94b33c8578b0 R08: 0000000000000002 R09: 00000000000201c0 [ 23.750664] R10: 00000006ecb0ccc6 R11: 0000000000034f38 R12: 000000000000316c [ 23.750670] R13: ffff94b33c84b250 R14: ffff94b33dedd5a0 R15: 0000000000000001 [ 23.750679] FS: 0000000000000000(0000) GS:ffff94b33e100000(0063) knlGS:00000000f7faf690 [ 23.750686] CS: 0010 DS: 002b ES: 002b CR0: 0000000080050033 [ 23.750691] CR2: 00000000f7f54faf CR3: 000000000722c000 CR4: 00000000001006e0 [ 23.750696] Call Trace: [ 23.750713] debug_dma_map_sg+0x100/0x340 [ 23.750727] ? dma_direct_map_sg+0x3b/0xb0 [ 23.750739] spi_map_buf+0x25a/0x300 [ 23.750751] __spi_pump_messages+0x2a4/0x680 [ 23.750762] __spi_sync+0x1dd/0x1f0 [ 23.750773] spi_sync+0x26/0x40 [ 23.750790] mipi_dbi_typec3_command_read+0x14d/0x240 [mipi_dbi] [ 23.750802] ? spi_finalize_current_transfer+0x10/0x10 [ 23.750821] mipi_dbi_typec3_command+0x1bc/0x1d0 [mipi_dbi] Reported-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Noralf Trønnes <noralf@tronnes.org> Tested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20190222124329.23046-1-noralf@tronnes.org
1 parent 8bfb175 commit a89bfc5

3 files changed

Lines changed: 48 additions & 21 deletions

File tree

‎drivers/gpu/drm/tinydrm/ili9225.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
317317
mipi->enabled = false;
318318
}
319319

320-
static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 cmd, u8 *par,
320+
static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par,
321321
size_t num)
322322
{
323323
struct spi_device *spi = mipi->spi;
@@ -327,11 +327,11 @@ static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 cmd, u8 *par,
327327

328328
gpiod_set_value_cansleep(mipi->dc, 0);
329329
speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
330-
ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
330+
ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, cmd, 1);
331331
if (ret || !num)
332332
return ret;
333333

334-
if (cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
334+
if (*cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
335335
bpw = 16;
336336

337337
gpiod_set_value_cansleep(mipi->dc, 1);

‎drivers/gpu/drm/tinydrm/mipi-dbi.c‎

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,42 @@ EXPORT_SYMBOL(mipi_dbi_command_read);
153153
*/
154154
int mipi_dbi_command_buf(struct mipi_dbi *mipi, u8 cmd, u8 *data, size_t len)
155155
{
156+
u8 *cmdbuf;
156157
int ret;
157158

159+
/* SPI requires dma-safe buffers */
160+
cmdbuf = kmemdup(&cmd, 1, GFP_KERNEL);
161+
if (!cmdbuf)
162+
return -ENOMEM;
163+
158164
mutex_lock(&mipi->cmdlock);
159-
ret = mipi->command(mipi, cmd, data, len);
165+
ret = mipi->command(mipi, cmdbuf, data, len);
160166
mutex_unlock(&mipi->cmdlock);
161167

168+
kfree(cmdbuf);
169+
162170
return ret;
163171
}
164172
EXPORT_SYMBOL(mipi_dbi_command_buf);
165173

174+
/* This should only be used by mipi_dbi_command() */
175+
int mipi_dbi_command_stackbuf(struct mipi_dbi *mipi, u8 cmd, u8 *data, size_t len)
176+
{
177+
u8 *buf;
178+
int ret;
179+
180+
buf = kmemdup(data, len, GFP_KERNEL);
181+
if (!buf)
182+
return -ENOMEM;
183+
184+
ret = mipi_dbi_command_buf(mipi, cmd, buf, len);
185+
186+
kfree(buf);
187+
188+
return ret;
189+
}
190+
EXPORT_SYMBOL(mipi_dbi_command_stackbuf);
191+
166192
/**
167193
* mipi_dbi_buf_copy - Copy a framebuffer, transforming it if necessary
168194
* @dst: The destination buffer
@@ -809,18 +835,18 @@ static int mipi_dbi_spi1_transfer(struct mipi_dbi *mipi, int dc,
809835
return 0;
810836
}
811837

812-
static int mipi_dbi_typec1_command(struct mipi_dbi *mipi, u8 cmd,
838+
static int mipi_dbi_typec1_command(struct mipi_dbi *mipi, u8 *cmd,
813839
u8 *parameters, size_t num)
814840
{
815-
unsigned int bpw = (cmd == MIPI_DCS_WRITE_MEMORY_START) ? 16 : 8;
841+
unsigned int bpw = (*cmd == MIPI_DCS_WRITE_MEMORY_START) ? 16 : 8;
816842
int ret;
817843

818-
if (mipi_dbi_command_is_read(mipi, cmd))
844+
if (mipi_dbi_command_is_read(mipi, *cmd))
819845
return -ENOTSUPP;
820846

821-
MIPI_DBI_DEBUG_COMMAND(cmd, parameters, num);
847+
MIPI_DBI_DEBUG_COMMAND(*cmd, parameters, num);
822848

823-
ret = mipi_dbi_spi1_transfer(mipi, 0, &cmd, 1, 8);
849+
ret = mipi_dbi_spi1_transfer(mipi, 0, cmd, 1, 8);
824850
if (ret || !num)
825851
return ret;
826852

@@ -829,7 +855,7 @@ static int mipi_dbi_typec1_command(struct mipi_dbi *mipi, u8 cmd,
829855

830856
/* MIPI DBI Type C Option 3 */
831857

832-
static int mipi_dbi_typec3_command_read(struct mipi_dbi *mipi, u8 cmd,
858+
static int mipi_dbi_typec3_command_read(struct mipi_dbi *mipi, u8 *cmd,
833859
u8 *data, size_t len)
834860
{
835861
struct spi_device *spi = mipi->spi;
@@ -838,7 +864,7 @@ static int mipi_dbi_typec3_command_read(struct mipi_dbi *mipi, u8 cmd,
838864
struct spi_transfer tr[2] = {
839865
{
840866
.speed_hz = speed_hz,
841-
.tx_buf = &cmd,
867+
.tx_buf = cmd,
842868
.len = 1,
843869
}, {
844870
.speed_hz = speed_hz,
@@ -856,8 +882,8 @@ static int mipi_dbi_typec3_command_read(struct mipi_dbi *mipi, u8 cmd,
856882
* Support non-standard 24-bit and 32-bit Nokia read commands which
857883
* start with a dummy clock, so we need to read an extra byte.
858884
*/
859-
if (cmd == MIPI_DCS_GET_DISPLAY_ID ||
860-
cmd == MIPI_DCS_GET_DISPLAY_STATUS) {
885+
if (*cmd == MIPI_DCS_GET_DISPLAY_ID ||
886+
*cmd == MIPI_DCS_GET_DISPLAY_STATUS) {
861887
if (!(len == 3 || len == 4))
862888
return -EINVAL;
863889

@@ -887,34 +913,34 @@ static int mipi_dbi_typec3_command_read(struct mipi_dbi *mipi, u8 cmd,
887913
data[i] = (buf[i] << 1) | !!(buf[i + 1] & BIT(7));
888914
}
889915

890-
MIPI_DBI_DEBUG_COMMAND(cmd, data, len);
916+
MIPI_DBI_DEBUG_COMMAND(*cmd, data, len);
891917

892918
err_free:
893919
kfree(buf);
894920

895921
return ret;
896922
}
897923

898-
static int mipi_dbi_typec3_command(struct mipi_dbi *mipi, u8 cmd,
924+
static int mipi_dbi_typec3_command(struct mipi_dbi *mipi, u8 *cmd,
899925
u8 *par, size_t num)
900926
{
901927
struct spi_device *spi = mipi->spi;
902928
unsigned int bpw = 8;
903929
u32 speed_hz;
904930
int ret;
905931

906-
if (mipi_dbi_command_is_read(mipi, cmd))
932+
if (mipi_dbi_command_is_read(mipi, *cmd))
907933
return mipi_dbi_typec3_command_read(mipi, cmd, par, num);
908934

909-
MIPI_DBI_DEBUG_COMMAND(cmd, par, num);
935+
MIPI_DBI_DEBUG_COMMAND(*cmd, par, num);
910936

911937
gpiod_set_value_cansleep(mipi->dc, 0);
912938
speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
913-
ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
939+
ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, cmd, 1);
914940
if (ret || !num)
915941
return ret;
916942

917-
if (cmd == MIPI_DCS_WRITE_MEMORY_START && !mipi->swap_bytes)
943+
if (*cmd == MIPI_DCS_WRITE_MEMORY_START && !mipi->swap_bytes)
918944
bpw = 16;
919945

920946
gpiod_set_value_cansleep(mipi->dc, 1);

‎include/drm/tinydrm/mipi-dbi.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct mipi_dbi {
5353
struct spi_device *spi;
5454
bool enabled;
5555
struct mutex cmdlock;
56-
int (*command)(struct mipi_dbi *mipi, u8 cmd, u8 *param, size_t num);
56+
int (*command)(struct mipi_dbi *mipi, u8 *cmd, u8 *param, size_t num);
5757
const u8 *read_commands;
5858
struct gpio_desc *dc;
5959
u16 *tx_buf;
@@ -91,6 +91,7 @@ u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len);
9191

9292
int mipi_dbi_command_read(struct mipi_dbi *mipi, u8 cmd, u8 *val);
9393
int mipi_dbi_command_buf(struct mipi_dbi *mipi, u8 cmd, u8 *data, size_t len);
94+
int mipi_dbi_command_stackbuf(struct mipi_dbi *mipi, u8 cmd, u8 *data, size_t len);
9495
int mipi_dbi_buf_copy(void *dst, struct drm_framebuffer *fb,
9596
struct drm_rect *clip, bool swap);
9697
/**
@@ -108,7 +109,7 @@ int mipi_dbi_buf_copy(void *dst, struct drm_framebuffer *fb,
108109
#define mipi_dbi_command(mipi, cmd, seq...) \
109110
({ \
110111
u8 d[] = { seq }; \
111-
mipi_dbi_command_buf(mipi, cmd, d, ARRAY_SIZE(d)); \
112+
mipi_dbi_command_stackbuf(mipi, cmd, d, ARRAY_SIZE(d)); \
112113
})
113114

114115
#ifdef CONFIG_DEBUG_FS

0 commit comments

Comments
 (0)