source: rtems/c/src/lib/libbsp/arm/raspberrypi/misc/vc.c @ 14de20f

Last change on this file since 14de20f was 14de20f, checked in by Pavel Pisa <pisa@…>, on Jun 24, 2016 at 9:22:43 AM

arm/raspberrypi: Force VC mail box buffer to be synchronized through cache.

This solution is quick fix until CPU_DATA_CACHE_ALIGNMENT is defined
and cache manager is checked on all Raspberry Pi variants.

  • Property mode set to 100644
File size: 9.9 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup raspberrypi_vc
5 *
6 * @brief video core support.
7 *
8 */
9
10/*
11 * Copyright (c) 2015 Yang Qiao
12 *
13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
15 *
16 *  http://www.rtems.org/license/LICENSE
17 *
18 */
19#include <pthread.h>
20
21#include <bsp.h>
22#include <bsp/raspberrypi.h>
23#include <bsp/mailbox.h>
24#include <bsp/vc.h>
25#include <libcpu/arm-cp15.h>
26#include "vc_defines.h"
27
28#if (RPI_L2_CACHE_ENABLE == 1)
29  #define BCM2835_VC_MEMORY_MAPPING 0x40000000
30#else
31  #define BCM2835_VC_MEMORY_MAPPING 0xC0000000
32#endif
33
34static inline bool
35bcm2835_mailbox_buffer_suceeded(const bcm2835_mbox_buf_hdr *hdr)
36{
37  RTEMS_COMPILER_MEMORY_BARRIER();
38  return (hdr->buf_code == BCM2835_MBOX_BUF_CODE_REQUEST_SUCCEED);
39}
40
41static inline int
42bcm2835_mailbox_send_read_buffer(void *buf)
43{
44  raspberrypi_mailbox_write(BCM2835_MBOX_CHANNEL_PROP_AVC,
45    (unsigned int)buf + BCM2835_VC_MEMORY_MAPPING);
46  raspberrypi_mailbox_read(BCM2835_MBOX_CHANNEL_PROP_AVC);
47  return 0;
48}
49
50/*
51 * When cache is enabled then content of buffer exchanged
52 * with VideoCore has to be propagated through ARM11/Cortex-A7
53 * caches
54 */
55static inline void
56bcm2835_mailbox_buffer_flush_and_invalidate(void *buf, size_t size)
57{
58  uint32_t sctlr_val;
59
60  sctlr_val = arm_cp15_get_control();
61
62  RTEMS_COMPILER_MEMORY_BARRIER();
63  arm_cp15_drain_write_buffer();
64  if (sctlr_val & (ARM_CP15_CTRL_C | ARM_CP15_CTRL_M)) {
65#if 0
66    /*
67    These architecture independent RTEMS API functions should be
68    used there but CPU_DATA_CACHE_ALIGNMENT is not defined
69    for ARM architecture version used on RPi and functions
70    are dummy for now and do not provide required synchronization
71    */
72    rtems_cache_flush_multiple_data_lines(buf, size);
73    rtems_cache_invalidate_multiple_data_lines(buf, size);
74#else
75    /* Flush complete data cache */
76    arm_cp15_data_cache_clean_and_invalidate();
77#endif
78  }
79}
80
81#define BCM2835_MBOX_VAL_LENGTH_MASK(_val_len) \
82  (_val_len&(~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE))
83
84int
85bcm2835_mailbox_get_display_size(bcm2835_get_display_size_entries *_entries)
86{
87  struct {
88    bcm2835_mbox_buf_hdr hdr;
89    bcm2835_mbox_tag_display_size get_display_size;
90    uint32_t end_tag;
91  }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
92  BCM2835_MBOX_INIT_BUF(&buffer);
93  BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_display_size,
94    BCM2835_MAILBOX_TAG_GET_DISPLAY_SIZE);
95  bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer));
96  if (bcm2835_mailbox_send_read_buffer(&buffer))
97    return -1;
98  _entries->width = buffer.get_display_size.body.resp.width;
99  _entries->height = buffer.get_display_size.body.resp.height;
100  if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) )
101    return -2;
102  return 0;
103}
104
105int
106bcm2835_mailbox_init_frame_buffer(bcm2835_init_frame_buffer_entries* _entries)
107{
108  struct {
109    bcm2835_mbox_buf_hdr hdr;
110    bcm2835_mbox_tag_display_size set_display_size;
111    bcm2835_mbox_tag_virtual_size set_virtual_size;
112    bcm2835_mbox_tag_depth set_depth;
113    bcm2835_mbox_tag_pixel_order set_pixel_order;
114    bcm2835_mbox_tag_alpha_mode set_alpha_mode;
115    bcm2835_mbox_tag_virtual_offset set_virtual_offset;
116    bcm2835_mbox_tag_overscan set_overscan;
117    bcm2835_mbox_tag_allocate_buffer allocate_buffer;
118    bcm2835_mbox_tag_get_pitch get_pitch;
119    uint32_t end_tag;
120  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
121  BCM2835_MBOX_INIT_BUF(&buffer);
122  BCM2835_MBOX_INIT_TAG(&buffer.set_display_size,
123    BCM2835_MAILBOX_TAG_SET_DISPLAY_SIZE);
124  buffer.set_display_size.body.req.width = _entries->xres;
125  buffer.set_display_size.body.req.height = _entries->yres;
126  BCM2835_MBOX_INIT_TAG(&buffer.set_virtual_size,
127    BCM2835_MAILBOX_TAG_SET_VIRTUAL_SIZE);
128  buffer.set_virtual_size.body.req.vwidth = _entries->xvirt;
129  buffer.set_virtual_size.body.req.vheight = _entries->yvirt;
130  BCM2835_MBOX_INIT_TAG(&buffer.set_depth,
131    BCM2835_MAILBOX_TAG_SET_DEPTH);
132  buffer.set_depth.body.req.depth = _entries->depth;
133  BCM2835_MBOX_INIT_TAG(&buffer.set_pixel_order,
134    BCM2835_MAILBOX_TAG_SET_PIXEL_ORDER);
135  buffer.set_pixel_order.body.req.pixel_order = _entries->pixel_order;
136  BCM2835_MBOX_INIT_TAG(&buffer.set_alpha_mode,
137    BCM2835_MAILBOX_TAG_SET_ALPHA_MODE);
138  buffer.set_alpha_mode.body.req.alpha_mode = _entries->alpha_mode;
139  BCM2835_MBOX_INIT_TAG(&buffer.set_virtual_offset,
140    BCM2835_MAILBOX_TAG_SET_VIRTUAL_OFFSET);
141  buffer.set_virtual_offset.body.req.voffset_x = _entries->voffset_x;
142  buffer.set_virtual_offset.body.req.voffset_x = _entries->voffset_y;
143  BCM2835_MBOX_INIT_TAG(&buffer.set_overscan,
144    BCM2835_MAILBOX_TAG_SET_OVERSCAN);
145  buffer.set_overscan.body.req.overscan_top = _entries->overscan_top;
146  buffer.set_overscan.body.req.overscan_bottom = _entries->overscan_bottom;
147  buffer.set_overscan.body.req.overscan_left = _entries->overscan_left;
148  buffer.set_overscan.body.req.overscan_right = _entries->overscan_right;
149  BCM2835_MBOX_INIT_TAG(&buffer.allocate_buffer,
150    BCM2835_MAILBOX_TAG_ALLOCATE_BUFFER);
151  buffer.allocate_buffer.body.req.align = 0x100;
152  BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_pitch,
153    BCM2835_MAILBOX_TAG_GET_PITCH);
154  bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer));
155  if (bcm2835_mailbox_send_read_buffer(&buffer))
156    return -1;
157  _entries->xres = buffer.set_display_size.body.resp.width;
158  _entries->yres = buffer.set_display_size.body.resp.height;
159  _entries->xvirt = buffer.set_virtual_size.body.resp.vwidth;
160  _entries->yvirt = buffer.set_virtual_size.body.resp.vheight;
161  _entries->depth = buffer.set_depth.body.resp.depth;
162#if (BSP_IS_RPI2 == 1)
163  _entries->base = buffer.allocate_buffer.body.resp.base;
164#else
165  _entries->base = buffer.allocate_buffer.body.resp.base
166                    - BCM2835_VC_MEMORY_MAPPING;
167#endif
168  _entries->size = buffer.allocate_buffer.body.resp.size;
169  _entries->pixel_order = buffer.set_pixel_order.body.resp.pixel_order;
170  _entries->alpha_mode = buffer.set_alpha_mode.body.resp.alpha_mode;
171  _entries->voffset_x = buffer.set_virtual_offset.body.resp.voffset_x;
172  _entries->voffset_y = buffer.set_virtual_offset.body.resp.voffset_y;
173  _entries->overscan_left = buffer.set_overscan.body.resp.overscan_left;
174  _entries->overscan_right = buffer.set_overscan.body.resp.overscan_right;
175  _entries->overscan_top = buffer.set_overscan.body.resp.overscan_top;
176  _entries->overscan_bottom = buffer.set_overscan.body.resp.overscan_bottom;
177  if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) )
178    return -2;
179  return 0;
180}
181
182int
183bcm2835_mailbox_get_pitch(bcm2835_get_pitch_entries* _entries)
184{
185  struct {
186    bcm2835_mbox_buf_hdr hdr;
187    bcm2835_mbox_tag_get_pitch get_pitch;
188    uint32_t end_tag;
189  }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
190  BCM2835_MBOX_INIT_BUF(&buffer);
191  BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_pitch,
192    BCM2835_MAILBOX_TAG_GET_PITCH);
193  bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer));
194  if (bcm2835_mailbox_send_read_buffer(&buffer))
195    return -1;
196  _entries->pitch = buffer.get_pitch.body.resp.pitch;
197  if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) )
198    return -2;
199  return 0;
200}
201
202int
203bcm2835_mailbox_get_cmdline(bcm2835_get_cmdline_entries* _entries)
204{
205  int i;
206  struct {
207    bcm2835_mbox_buf_hdr hdr;
208    bcm2835_mbox_tag_get_cmd_line get_cmd_line;
209    uint32_t end_tag;
210  }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
211  BCM2835_MBOX_INIT_BUF(&buffer);
212  BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_cmd_line,
213    BCM2835_MAILBOX_TAG_GET_CMD_LINE);
214  bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer));
215  if (bcm2835_mailbox_send_read_buffer(&buffer))
216    return -1;
217  for (i = 0;
218        i < BCM2835_MBOX_VAL_LENGTH_MASK(buffer.get_cmd_line.tag_hdr.val_len);
219        ++i)
220  {
221    _entries->cmdline[i] = buffer.get_cmd_line.body.resp.cmdline[i];
222  }
223  _entries->cmdline[i] = '\0';
224  if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) )
225    return -2;
226  return 0;
227}
228
229int
230bcm2835_mailbox_set_power_state(bcm2835_set_power_state_entries *_entries)
231{
232  struct {
233    bcm2835_mbox_buf_hdr hdr;
234    bcm2835_mbox_tag_power_state set_power_state;
235    uint32_t end_tag;
236  }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
237  BCM2835_MBOX_INIT_BUF(&buffer);
238  BCM2835_MBOX_INIT_TAG(&buffer.set_power_state,
239    BCM2835_MAILBOX_TAG_SET_POWER_STATE);
240  buffer.set_power_state.body.req.dev_id = _entries->dev_id;
241  buffer.set_power_state.body.req.state = _entries->state;
242  bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer));
243  if (bcm2835_mailbox_send_read_buffer(&buffer))
244    return -1;
245  _entries->dev_id = buffer.set_power_state.body.resp.dev_id;
246  _entries->state  = buffer.set_power_state.body.resp.state;
247  if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) )
248    return -2;
249  return 0;
250}
251
252int
253bcm2835_mailbox_get_arm_memory(bcm2835_get_arm_memory_entries* _entries)
254{
255  struct {
256    bcm2835_mbox_buf_hdr hdr;
257    bcm2835_mbox_tag_get_arm_memory get_arm_memory;
258    uint32_t end_tag;
259  }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
260  BCM2835_MBOX_INIT_BUF(&buffer);
261  BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_arm_memory,
262    BCM2835_MAILBOX_TAG_GET_ARM_MEMORY);
263  bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer));
264  if (bcm2835_mailbox_send_read_buffer(&buffer))
265    return -1;
266  _entries->base = buffer.get_arm_memory.body.resp.base;
267  _entries->size = buffer.get_arm_memory.body.resp.size;
268  if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) )
269    return -2;
270  return 0;
271}
272
273int
274bcm2835_mailbox_get_vc_memory(bcm2835_get_vc_memory_entries* _entries)
275{
276  struct {
277    bcm2835_mbox_buf_hdr hdr;
278    bcm2835_mbox_tag_get_vc_memory get_vc_memory;
279    uint32_t end_tag;
280  }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
281  BCM2835_MBOX_INIT_BUF(&buffer);
282  BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_vc_memory,
283    BCM2835_MAILBOX_TAG_GET_VC_MEMORY);
284  bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer));
285  if (bcm2835_mailbox_send_read_buffer(&buffer))
286    return -1;
287  _entries->base = buffer.get_vc_memory.body.resp.base;
288  _entries->size = buffer.get_vc_memory.body.resp.size;
289  if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) )
290    return -2;
291  return 0;
292}
Note: See TracBrowser for help on using the repository browser.