source: rtems/c/src/lib/libbsp/arm/raspberrypi/misc/vc.c @ 2d5902d6

5
Last change on this file since 2d5902d6 was 2d5902d6, checked in by Pavel Pisa <pisa@…>, on Jul 2, 2016 at 4:52:44 PM

arm/raspberrypi: update VideoCore? cache flush workaround to work on RPi2.

The arm_cp15_data_cache_clean_and_invalidate leads to hang on RPi2,
clean by individual lines works on RPi1 and RPi2.

  • Property mode set to 100644
File size: 12.4 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 bcm2835_mailbox_buffer_suceeded(
35  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 bcm2835_mailbox_send_read_buffer( void *buf )
42{
43  raspberrypi_mailbox_write( BCM2835_MBOX_CHANNEL_PROP_AVC,
44    (unsigned int) buf + BCM2835_VC_MEMORY_MAPPING );
45  raspberrypi_mailbox_read( BCM2835_MBOX_CHANNEL_PROP_AVC );
46  return 0;
47}
48
49/*
50 * When cache is enabled then content of buffer exchanged
51 * with VideoCore has to be propagated through ARM11/Cortex-A7
52 * caches
53 */
54static inline void bcm2835_mailbox_buffer_flush_and_invalidate(
55  void  *buf,
56  size_t size
57)
58{
59  uint32_t sctlr_val;
60
61  sctlr_val = arm_cp15_get_control();
62
63  RTEMS_COMPILER_MEMORY_BARRIER();
64  arm_cp15_drain_write_buffer();
65
66  if ( sctlr_val & ( ARM_CP15_CTRL_C | ARM_CP15_CTRL_M ) ) {
67#if 0
68    /*
69       These architecture independent RTEMS API functions should be
70       used there but CPU_DATA_CACHE_ALIGNMENT is not defined
71       for ARM architecture version used on RPi and functions
72       are dummy for now and do not provide required synchronization
73     */
74    rtems_cache_flush_multiple_data_lines( buf, size );
75    rtems_cache_invalidate_multiple_data_lines( buf, size );
76#elif 0
77    /* Flush complete data cache, does not work on RPi2 for some reason */
78    arm_cp15_data_cache_clean_and_invalidate();
79#else
80    /*
81     * This is temporal workaround for missing cache meanager
82     * which works on RPi2
83     */
84    size += (uintptr_t)buf & ~63;
85    size = (size + 63) & ~63;
86    while ( size ) {
87      size -= 32;
88      arm_cp15_data_cache_clean_and_invalidate_line(buf);
89    }
90#endif
91  }
92}
93
94#define BCM2835_MBOX_VAL_LENGTH_MASK( _val_len ) \
95  ( _val_len & ( ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE ) )
96
97int bcm2835_mailbox_get_display_size(
98  bcm2835_get_display_size_entries *_entries )
99{
100  struct {
101    bcm2835_mbox_buf_hdr hdr;
102    bcm2835_mbox_tag_display_size get_display_size;
103    uint32_t end_tag;
104  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
105  BCM2835_MBOX_INIT_BUF( &buffer );
106  BCM2835_MBOX_INIT_TAG_NO_REQ( &buffer.get_display_size,
107    BCM2835_MAILBOX_TAG_GET_DISPLAY_SIZE );
108  bcm2835_mailbox_buffer_flush_and_invalidate( &buffer, sizeof( &buffer ) );
109
110  if ( bcm2835_mailbox_send_read_buffer( &buffer ) )
111    return -1;
112
113  _entries->width = buffer.get_display_size.body.resp.width;
114  _entries->height = buffer.get_display_size.body.resp.height;
115
116  if ( !bcm2835_mailbox_buffer_suceeded( &buffer.hdr ) )
117    return -2;
118
119  return 0;
120}
121
122int bcm2835_mailbox_init_frame_buffer(
123  bcm2835_init_frame_buffer_entries *_entries )
124{
125  struct {
126    bcm2835_mbox_buf_hdr hdr;
127    bcm2835_mbox_tag_display_size set_display_size;
128    bcm2835_mbox_tag_virtual_size set_virtual_size;
129    bcm2835_mbox_tag_depth set_depth;
130    bcm2835_mbox_tag_pixel_order set_pixel_order;
131    bcm2835_mbox_tag_alpha_mode set_alpha_mode;
132    bcm2835_mbox_tag_virtual_offset set_virtual_offset;
133    bcm2835_mbox_tag_overscan set_overscan;
134    bcm2835_mbox_tag_allocate_buffer allocate_buffer;
135    bcm2835_mbox_tag_get_pitch get_pitch;
136    uint32_t end_tag;
137  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
138  BCM2835_MBOX_INIT_BUF( &buffer );
139  BCM2835_MBOX_INIT_TAG( &buffer.set_display_size,
140    BCM2835_MAILBOX_TAG_SET_DISPLAY_SIZE );
141  buffer.set_display_size.body.req.width = _entries->xres;
142  buffer.set_display_size.body.req.height = _entries->yres;
143  BCM2835_MBOX_INIT_TAG( &buffer.set_virtual_size,
144    BCM2835_MAILBOX_TAG_SET_VIRTUAL_SIZE );
145  buffer.set_virtual_size.body.req.vwidth = _entries->xvirt;
146  buffer.set_virtual_size.body.req.vheight = _entries->yvirt;
147  BCM2835_MBOX_INIT_TAG( &buffer.set_depth,
148    BCM2835_MAILBOX_TAG_SET_DEPTH );
149  buffer.set_depth.body.req.depth = _entries->depth;
150  BCM2835_MBOX_INIT_TAG( &buffer.set_pixel_order,
151    BCM2835_MAILBOX_TAG_SET_PIXEL_ORDER );
152  buffer.set_pixel_order.body.req.pixel_order = _entries->pixel_order;
153  BCM2835_MBOX_INIT_TAG( &buffer.set_alpha_mode,
154    BCM2835_MAILBOX_TAG_SET_ALPHA_MODE );
155  buffer.set_alpha_mode.body.req.alpha_mode = _entries->alpha_mode;
156  BCM2835_MBOX_INIT_TAG( &buffer.set_virtual_offset,
157    BCM2835_MAILBOX_TAG_SET_VIRTUAL_OFFSET );
158  buffer.set_virtual_offset.body.req.voffset_x = _entries->voffset_x;
159  buffer.set_virtual_offset.body.req.voffset_x = _entries->voffset_y;
160  BCM2835_MBOX_INIT_TAG( &buffer.set_overscan,
161    BCM2835_MAILBOX_TAG_SET_OVERSCAN );
162  buffer.set_overscan.body.req.overscan_top = _entries->overscan_top;
163  buffer.set_overscan.body.req.overscan_bottom = _entries->overscan_bottom;
164  buffer.set_overscan.body.req.overscan_left = _entries->overscan_left;
165  buffer.set_overscan.body.req.overscan_right = _entries->overscan_right;
166  BCM2835_MBOX_INIT_TAG( &buffer.allocate_buffer,
167    BCM2835_MAILBOX_TAG_ALLOCATE_BUFFER );
168  buffer.allocate_buffer.body.req.align = 0x100;
169  BCM2835_MBOX_INIT_TAG_NO_REQ( &buffer.get_pitch,
170    BCM2835_MAILBOX_TAG_GET_PITCH );
171  bcm2835_mailbox_buffer_flush_and_invalidate( &buffer, sizeof( &buffer ) );
172
173  if ( bcm2835_mailbox_send_read_buffer( &buffer ) )
174    return -1;
175
176  _entries->xres = buffer.set_display_size.body.resp.width;
177  _entries->yres = buffer.set_display_size.body.resp.height;
178  _entries->xvirt = buffer.set_virtual_size.body.resp.vwidth;
179  _entries->yvirt = buffer.set_virtual_size.body.resp.vheight;
180  _entries->depth = buffer.set_depth.body.resp.depth;
181#if ( BSP_IS_RPI2 == 1 )
182  _entries->base = buffer.allocate_buffer.body.resp.base;
183#else
184  _entries->base = buffer.allocate_buffer.body.resp.base -
185                   BCM2835_VC_MEMORY_MAPPING;
186#endif
187  _entries->size = buffer.allocate_buffer.body.resp.size;
188  _entries->pixel_order = buffer.set_pixel_order.body.resp.pixel_order;
189  _entries->alpha_mode = buffer.set_alpha_mode.body.resp.alpha_mode;
190  _entries->voffset_x = buffer.set_virtual_offset.body.resp.voffset_x;
191  _entries->voffset_y = buffer.set_virtual_offset.body.resp.voffset_y;
192  _entries->overscan_left = buffer.set_overscan.body.resp.overscan_left;
193  _entries->overscan_right = buffer.set_overscan.body.resp.overscan_right;
194  _entries->overscan_top = buffer.set_overscan.body.resp.overscan_top;
195  _entries->overscan_bottom = buffer.set_overscan.body.resp.overscan_bottom;
196
197  if ( !bcm2835_mailbox_buffer_suceeded( &buffer.hdr ) )
198    return -2;
199
200  return 0;
201}
202
203int bcm2835_mailbox_get_pitch( bcm2835_get_pitch_entries *_entries )
204{
205  struct {
206    bcm2835_mbox_buf_hdr hdr;
207    bcm2835_mbox_tag_get_pitch get_pitch;
208    uint32_t end_tag;
209  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
210  BCM2835_MBOX_INIT_BUF( &buffer );
211  BCM2835_MBOX_INIT_TAG_NO_REQ( &buffer.get_pitch,
212    BCM2835_MAILBOX_TAG_GET_PITCH );
213  bcm2835_mailbox_buffer_flush_and_invalidate( &buffer, sizeof( &buffer ) );
214
215  if ( bcm2835_mailbox_send_read_buffer( &buffer ) )
216    return -1;
217
218  _entries->pitch = buffer.get_pitch.body.resp.pitch;
219
220  if ( !bcm2835_mailbox_buffer_suceeded( &buffer.hdr ) )
221    return -2;
222
223  return 0;
224}
225
226int bcm2835_mailbox_get_cmdline( bcm2835_get_cmdline_entries *_entries )
227{
228  int i;
229
230  struct {
231    bcm2835_mbox_buf_hdr hdr;
232    bcm2835_mbox_tag_get_cmd_line get_cmd_line;
233    uint32_t end_tag;
234  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
235  BCM2835_MBOX_INIT_BUF( &buffer );
236  BCM2835_MBOX_INIT_TAG_NO_REQ( &buffer.get_cmd_line,
237    BCM2835_MAILBOX_TAG_GET_CMD_LINE );
238  bcm2835_mailbox_buffer_flush_and_invalidate( &buffer, sizeof( &buffer ) );
239
240  if ( bcm2835_mailbox_send_read_buffer( &buffer ) )
241    return -1;
242
243  for ( i = 0;
244        i < BCM2835_MBOX_VAL_LENGTH_MASK( buffer.get_cmd_line.tag_hdr.val_len );
245        ++i ) {
246    _entries->cmdline[ i ] = buffer.get_cmd_line.body.resp.cmdline[ i ];
247  }
248
249  _entries->cmdline[ i ] = '\0';
250
251  if ( !bcm2835_mailbox_buffer_suceeded( &buffer.hdr ) )
252    return -2;
253
254  return 0;
255}
256
257int bcm2835_mailbox_set_power_state( bcm2835_set_power_state_entries *_entries )
258{
259  struct {
260    bcm2835_mbox_buf_hdr hdr;
261    bcm2835_mbox_tag_power_state set_power_state;
262    uint32_t end_tag;
263  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
264  BCM2835_MBOX_INIT_BUF( &buffer );
265  BCM2835_MBOX_INIT_TAG( &buffer.set_power_state,
266    BCM2835_MAILBOX_TAG_SET_POWER_STATE );
267  buffer.set_power_state.body.req.dev_id = _entries->dev_id;
268  buffer.set_power_state.body.req.state = _entries->state;
269  bcm2835_mailbox_buffer_flush_and_invalidate( &buffer, sizeof( &buffer ) );
270
271  if ( bcm2835_mailbox_send_read_buffer( &buffer ) )
272    return -1;
273
274  _entries->dev_id = buffer.set_power_state.body.resp.dev_id;
275  _entries->state = buffer.set_power_state.body.resp.state;
276
277  if ( !bcm2835_mailbox_buffer_suceeded( &buffer.hdr ) )
278    return -2;
279
280  return 0;
281}
282
283int bcm2835_mailbox_get_arm_memory( bcm2835_get_arm_memory_entries *_entries )
284{
285  struct {
286    bcm2835_mbox_buf_hdr hdr;
287    bcm2835_mbox_tag_get_arm_memory get_arm_memory;
288    uint32_t end_tag;
289  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
290  BCM2835_MBOX_INIT_BUF( &buffer );
291  BCM2835_MBOX_INIT_TAG_NO_REQ( &buffer.get_arm_memory,
292    BCM2835_MAILBOX_TAG_GET_ARM_MEMORY );
293  bcm2835_mailbox_buffer_flush_and_invalidate( &buffer, sizeof( &buffer ) );
294
295  if ( bcm2835_mailbox_send_read_buffer( &buffer ) )
296    return -1;
297
298  _entries->base = buffer.get_arm_memory.body.resp.base;
299  _entries->size = buffer.get_arm_memory.body.resp.size;
300
301  if ( !bcm2835_mailbox_buffer_suceeded( &buffer.hdr ) )
302    return -2;
303
304  return 0;
305}
306
307int bcm2835_mailbox_get_vc_memory( bcm2835_get_vc_memory_entries *_entries )
308{
309  struct {
310    bcm2835_mbox_buf_hdr hdr;
311    bcm2835_mbox_tag_get_vc_memory get_vc_memory;
312    uint32_t end_tag;
313  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
314  BCM2835_MBOX_INIT_BUF( &buffer );
315  BCM2835_MBOX_INIT_TAG_NO_REQ( &buffer.get_vc_memory,
316    BCM2835_MAILBOX_TAG_GET_VC_MEMORY );
317  bcm2835_mailbox_buffer_flush_and_invalidate( &buffer, sizeof( &buffer ) );
318
319  if ( bcm2835_mailbox_send_read_buffer( &buffer ) )
320    return -1;
321
322  _entries->base = buffer.get_vc_memory.body.resp.base;
323  _entries->size = buffer.get_vc_memory.body.resp.size;
324
325  if ( !bcm2835_mailbox_buffer_suceeded( &buffer.hdr ) )
326    return -2;
327
328  return 0;
329}
330
331int bcm2835_mailbox_get_firmware_revision(
332  bcm2835_mailbox_get_fw_rev_entries *_entries )
333{
334  struct {
335    bcm2835_mbox_buf_hdr hdr;
336    bcm2835_mbox_tag_get_fw_rev get_fw_rev;
337    uint32_t end_tag;
338  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
339  BCM2835_MBOX_INIT_BUF( &buffer );
340  BCM2835_MBOX_INIT_TAG_NO_REQ( &buffer.get_fw_rev,
341    BCM2835_MAILBOX_TAG_FIRMWARE_REVISION );
342  bcm2835_mailbox_buffer_flush_and_invalidate( &buffer, sizeof( &buffer ) );
343
344  if ( bcm2835_mailbox_send_read_buffer( &buffer ) )
345    return -1;
346
347  _entries->fw_rev = buffer.get_fw_rev.body.resp.rev;
348
349  if ( !bcm2835_mailbox_buffer_suceeded( &buffer.hdr ) )
350    return -2;
351
352  return 0;
353}
354
355int bcm2835_mailbox_get_board_model( bcm2835_get_board_spec_entries *_entries )
356{
357  struct {
358    bcm2835_mbox_buf_hdr hdr;
359    bcm2835_mbox_tag_get_board_spec get_board_model;
360    uint32_t end_tag;
361  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
362  BCM2835_MBOX_INIT_BUF( &buffer );
363  BCM2835_MBOX_INIT_TAG_NO_REQ( &buffer.get_board_model,
364    BCM2835_MAILBOX_TAG_GET_BOARD_MODEL );
365  bcm2835_mailbox_buffer_flush_and_invalidate( &buffer, sizeof( &buffer ) );
366
367  if ( bcm2835_mailbox_send_read_buffer( &buffer ) )
368    return -1;
369
370  _entries->spec = buffer.get_board_model.body.resp.spec;
371
372  if ( !bcm2835_mailbox_buffer_suceeded( &buffer.hdr ) )
373    return -2;
374
375  return 0;
376}
377
378int bcm2835_mailbox_get_board_revision(
379  bcm2835_get_board_spec_entries *_entries )
380{
381  struct {
382    bcm2835_mbox_buf_hdr hdr;
383    bcm2835_mbox_tag_get_board_spec get_board_revision;
384    uint32_t end_tag;
385  } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE;
386  BCM2835_MBOX_INIT_BUF( &buffer );
387  BCM2835_MBOX_INIT_TAG_NO_REQ( &buffer.get_board_revision,
388    BCM2835_MAILBOX_TAG_GET_BOARD_VERSION );
389  bcm2835_mailbox_buffer_flush_and_invalidate( &buffer, sizeof( &buffer ) );
390
391  if ( bcm2835_mailbox_send_read_buffer( &buffer ) )
392    return -1;
393
394  _entries->spec = buffer.get_board_revision.body.resp.spec;
395
396  if ( !bcm2835_mailbox_buffer_suceeded( &buffer.hdr ) )
397    return -2;
398
399  return 0;
400}
Note: See TracBrowser for help on using the repository browser.