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 | |
---|
34 | static inline bool |
---|
35 | bcm2835_mailbox_buffer_suceeded(const bcm2835_mbox_buf_hdr *hdr) |
---|
36 | { |
---|
37 | return (hdr->buf_code == BCM2835_MBOX_BUF_CODE_REQUEST_SUCCEED); |
---|
38 | } |
---|
39 | |
---|
40 | static inline int |
---|
41 | 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 | */ |
---|
54 | static inline void |
---|
55 | bcm2835_mailbox_buffer_flush_and_invalidate(void *buf, size_t size) |
---|
56 | { |
---|
57 | uint32_t sctlr_val; |
---|
58 | |
---|
59 | sctlr_val = arm_cp15_get_control(); |
---|
60 | |
---|
61 | arm_cp15_drain_write_buffer(); |
---|
62 | if (sctlr_val & (ARM_CP15_CTRL_C | ARM_CP15_CTRL_M)) { |
---|
63 | arm_cp15_drain_write_buffer(); |
---|
64 | rtems_cache_flush_multiple_data_lines(buf, size); |
---|
65 | rtems_cache_invalidate_multiple_data_lines(buf, size); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | #define BCM2835_MBOX_VAL_LENGTH_MASK(_val_len) \ |
---|
70 | (_val_len&(~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE)) |
---|
71 | |
---|
72 | int |
---|
73 | bcm2835_mailbox_get_display_size(bcm2835_get_display_size_entries *_entries) |
---|
74 | { |
---|
75 | struct { |
---|
76 | bcm2835_mbox_buf_hdr hdr; |
---|
77 | bcm2835_mbox_tag_display_size get_display_size; |
---|
78 | uint32_t end_tag; |
---|
79 | }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE; |
---|
80 | BCM2835_MBOX_INIT_BUF(&buffer); |
---|
81 | BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_display_size, |
---|
82 | BCM2835_MAILBOX_TAG_GET_DISPLAY_SIZE); |
---|
83 | bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer)); |
---|
84 | if (bcm2835_mailbox_send_read_buffer(&buffer)) |
---|
85 | return -1; |
---|
86 | _entries->width = buffer.get_display_size.body.resp.width; |
---|
87 | _entries->height = buffer.get_display_size.body.resp.height; |
---|
88 | if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) ) |
---|
89 | return -2; |
---|
90 | return 0; |
---|
91 | } |
---|
92 | |
---|
93 | int |
---|
94 | bcm2835_mailbox_init_frame_buffer(bcm2835_init_frame_buffer_entries* _entries) |
---|
95 | { |
---|
96 | struct { |
---|
97 | bcm2835_mbox_buf_hdr hdr; |
---|
98 | bcm2835_mbox_tag_display_size set_display_size; |
---|
99 | bcm2835_mbox_tag_virtual_size set_virtual_size; |
---|
100 | bcm2835_mbox_tag_depth set_depth; |
---|
101 | bcm2835_mbox_tag_pixel_order set_pixel_order; |
---|
102 | bcm2835_mbox_tag_alpha_mode set_alpha_mode; |
---|
103 | bcm2835_mbox_tag_virtual_offset set_virtual_offset; |
---|
104 | bcm2835_mbox_tag_overscan set_overscan; |
---|
105 | bcm2835_mbox_tag_allocate_buffer allocate_buffer; |
---|
106 | bcm2835_mbox_tag_get_pitch get_pitch; |
---|
107 | uint32_t end_tag; |
---|
108 | } buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE; |
---|
109 | BCM2835_MBOX_INIT_BUF(&buffer); |
---|
110 | BCM2835_MBOX_INIT_TAG(&buffer.set_display_size, |
---|
111 | BCM2835_MAILBOX_TAG_SET_DISPLAY_SIZE); |
---|
112 | buffer.set_display_size.body.req.width = _entries->xres; |
---|
113 | buffer.set_display_size.body.req.height = _entries->yres; |
---|
114 | BCM2835_MBOX_INIT_TAG(&buffer.set_virtual_size, |
---|
115 | BCM2835_MAILBOX_TAG_SET_VIRTUAL_SIZE); |
---|
116 | buffer.set_virtual_size.body.req.vwidth = _entries->xvirt; |
---|
117 | buffer.set_virtual_size.body.req.vheight = _entries->yvirt; |
---|
118 | BCM2835_MBOX_INIT_TAG(&buffer.set_depth, |
---|
119 | BCM2835_MAILBOX_TAG_SET_DEPTH); |
---|
120 | buffer.set_depth.body.req.depth = _entries->depth; |
---|
121 | BCM2835_MBOX_INIT_TAG(&buffer.set_pixel_order, |
---|
122 | BCM2835_MAILBOX_TAG_SET_PIXEL_ORDER); |
---|
123 | buffer.set_pixel_order.body.req.pixel_order = _entries->pixel_order; |
---|
124 | BCM2835_MBOX_INIT_TAG(&buffer.set_alpha_mode, |
---|
125 | BCM2835_MAILBOX_TAG_SET_ALPHA_MODE); |
---|
126 | buffer.set_alpha_mode.body.req.alpha_mode = _entries->alpha_mode; |
---|
127 | BCM2835_MBOX_INIT_TAG(&buffer.set_virtual_offset, |
---|
128 | BCM2835_MAILBOX_TAG_SET_VIRTUAL_OFFSET); |
---|
129 | buffer.set_virtual_offset.body.req.voffset_x = _entries->voffset_x; |
---|
130 | buffer.set_virtual_offset.body.req.voffset_x = _entries->voffset_y; |
---|
131 | BCM2835_MBOX_INIT_TAG(&buffer.set_overscan, |
---|
132 | BCM2835_MAILBOX_TAG_SET_OVERSCAN); |
---|
133 | buffer.set_overscan.body.req.overscan_top = _entries->overscan_top; |
---|
134 | buffer.set_overscan.body.req.overscan_bottom = _entries->overscan_bottom; |
---|
135 | buffer.set_overscan.body.req.overscan_left = _entries->overscan_left; |
---|
136 | buffer.set_overscan.body.req.overscan_right = _entries->overscan_right; |
---|
137 | BCM2835_MBOX_INIT_TAG(&buffer.allocate_buffer, |
---|
138 | BCM2835_MAILBOX_TAG_ALLOCATE_BUFFER); |
---|
139 | buffer.allocate_buffer.body.req.align = 0x100; |
---|
140 | BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_pitch, |
---|
141 | BCM2835_MAILBOX_TAG_GET_PITCH); |
---|
142 | bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer)); |
---|
143 | if (bcm2835_mailbox_send_read_buffer(&buffer)) |
---|
144 | return -1; |
---|
145 | _entries->xres = buffer.set_display_size.body.resp.width; |
---|
146 | _entries->yres = buffer.set_display_size.body.resp.height; |
---|
147 | _entries->xvirt = buffer.set_virtual_size.body.resp.vwidth; |
---|
148 | _entries->yvirt = buffer.set_virtual_size.body.resp.vheight; |
---|
149 | _entries->depth = buffer.set_depth.body.resp.depth; |
---|
150 | #if (BSP_IS_RPI2 == 1) |
---|
151 | _entries->base = buffer.allocate_buffer.body.resp.base; |
---|
152 | #else |
---|
153 | _entries->base = buffer.allocate_buffer.body.resp.base |
---|
154 | - BCM2835_VC_MEMORY_MAPPING; |
---|
155 | #endif |
---|
156 | _entries->size = buffer.allocate_buffer.body.resp.size; |
---|
157 | _entries->pixel_order = buffer.set_pixel_order.body.resp.pixel_order; |
---|
158 | _entries->alpha_mode = buffer.set_alpha_mode.body.resp.alpha_mode; |
---|
159 | _entries->voffset_x = buffer.set_virtual_offset.body.resp.voffset_x; |
---|
160 | _entries->voffset_y = buffer.set_virtual_offset.body.resp.voffset_y; |
---|
161 | _entries->overscan_left = buffer.set_overscan.body.resp.overscan_left; |
---|
162 | _entries->overscan_right = buffer.set_overscan.body.resp.overscan_right; |
---|
163 | _entries->overscan_top = buffer.set_overscan.body.resp.overscan_top; |
---|
164 | _entries->overscan_bottom = buffer.set_overscan.body.resp.overscan_bottom; |
---|
165 | if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) ) |
---|
166 | return -2; |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | int |
---|
171 | bcm2835_mailbox_get_pitch(bcm2835_get_pitch_entries* _entries) |
---|
172 | { |
---|
173 | struct { |
---|
174 | bcm2835_mbox_buf_hdr hdr; |
---|
175 | bcm2835_mbox_tag_get_pitch get_pitch; |
---|
176 | uint32_t end_tag; |
---|
177 | }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE; |
---|
178 | BCM2835_MBOX_INIT_BUF(&buffer); |
---|
179 | BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_pitch, |
---|
180 | BCM2835_MAILBOX_TAG_GET_PITCH); |
---|
181 | bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer)); |
---|
182 | if (bcm2835_mailbox_send_read_buffer(&buffer)) |
---|
183 | return -1; |
---|
184 | _entries->pitch = buffer.get_pitch.body.resp.pitch; |
---|
185 | if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) ) |
---|
186 | return -2; |
---|
187 | return 0; |
---|
188 | } |
---|
189 | |
---|
190 | int |
---|
191 | bcm2835_mailbox_get_cmdline(bcm2835_get_cmdline_entries* _entries) |
---|
192 | { |
---|
193 | int i; |
---|
194 | struct { |
---|
195 | bcm2835_mbox_buf_hdr hdr; |
---|
196 | bcm2835_mbox_tag_get_cmd_line get_cmd_line; |
---|
197 | uint32_t end_tag; |
---|
198 | }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE; |
---|
199 | BCM2835_MBOX_INIT_BUF(&buffer); |
---|
200 | BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_cmd_line, |
---|
201 | BCM2835_MAILBOX_TAG_GET_CMD_LINE); |
---|
202 | bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer)); |
---|
203 | if (bcm2835_mailbox_send_read_buffer(&buffer)) |
---|
204 | return -1; |
---|
205 | for (i = 0; |
---|
206 | i < BCM2835_MBOX_VAL_LENGTH_MASK(buffer.get_cmd_line.tag_hdr.val_len); |
---|
207 | ++i) |
---|
208 | { |
---|
209 | _entries->cmdline[i] = buffer.get_cmd_line.body.resp.cmdline[i]; |
---|
210 | } |
---|
211 | _entries->cmdline[i] = '\0'; |
---|
212 | if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) ) |
---|
213 | return -2; |
---|
214 | return 0; |
---|
215 | } |
---|
216 | |
---|
217 | int |
---|
218 | bcm2835_mailbox_set_power_state(bcm2835_set_power_state_entries *_entries) |
---|
219 | { |
---|
220 | struct { |
---|
221 | bcm2835_mbox_buf_hdr hdr; |
---|
222 | bcm2835_mbox_tag_power_state set_power_state; |
---|
223 | uint32_t end_tag; |
---|
224 | }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE; |
---|
225 | BCM2835_MBOX_INIT_BUF(&buffer); |
---|
226 | BCM2835_MBOX_INIT_TAG(&buffer.set_power_state, |
---|
227 | BCM2835_MAILBOX_TAG_SET_POWER_STATE); |
---|
228 | buffer.set_power_state.body.req.dev_id = _entries->dev_id; |
---|
229 | buffer.set_power_state.body.req.state = _entries->state; |
---|
230 | bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer)); |
---|
231 | if (bcm2835_mailbox_send_read_buffer(&buffer)) |
---|
232 | return -1; |
---|
233 | _entries->dev_id = buffer.set_power_state.body.resp.dev_id; |
---|
234 | _entries->state = buffer.set_power_state.body.resp.state; |
---|
235 | if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) ) |
---|
236 | return -2; |
---|
237 | return 0; |
---|
238 | } |
---|
239 | |
---|
240 | int |
---|
241 | bcm2835_mailbox_get_arm_memory(bcm2835_get_arm_memory_entries* _entries) |
---|
242 | { |
---|
243 | struct { |
---|
244 | bcm2835_mbox_buf_hdr hdr; |
---|
245 | bcm2835_mbox_tag_get_arm_memory get_arm_memory; |
---|
246 | uint32_t end_tag; |
---|
247 | }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE; |
---|
248 | BCM2835_MBOX_INIT_BUF(&buffer); |
---|
249 | BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_arm_memory, |
---|
250 | BCM2835_MAILBOX_TAG_GET_ARM_MEMORY); |
---|
251 | bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer)); |
---|
252 | if (bcm2835_mailbox_send_read_buffer(&buffer)) |
---|
253 | return -1; |
---|
254 | _entries->base = buffer.get_arm_memory.body.resp.base; |
---|
255 | _entries->size = buffer.get_arm_memory.body.resp.size; |
---|
256 | if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) ) |
---|
257 | return -2; |
---|
258 | return 0; |
---|
259 | } |
---|
260 | |
---|
261 | int |
---|
262 | bcm2835_mailbox_get_vc_memory(bcm2835_get_vc_memory_entries* _entries) |
---|
263 | { |
---|
264 | struct { |
---|
265 | bcm2835_mbox_buf_hdr hdr; |
---|
266 | bcm2835_mbox_tag_get_vc_memory get_vc_memory; |
---|
267 | uint32_t end_tag; |
---|
268 | }buffer BCM2835_MBOX_BUF_ALIGN_ATTRIBUTE; |
---|
269 | BCM2835_MBOX_INIT_BUF(&buffer); |
---|
270 | BCM2835_MBOX_INIT_TAG_NO_REQ(&buffer.get_vc_memory, |
---|
271 | BCM2835_MAILBOX_TAG_GET_VC_MEMORY); |
---|
272 | bcm2835_mailbox_buffer_flush_and_invalidate(&buffer, sizeof(&buffer)); |
---|
273 | if (bcm2835_mailbox_send_read_buffer(&buffer)) |
---|
274 | return -1; |
---|
275 | _entries->base = buffer.get_vc_memory.body.resp.base; |
---|
276 | _entries->size = buffer.get_vc_memory.body.resp.size; |
---|
277 | if( !bcm2835_mailbox_buffer_suceeded(&buffer.hdr) ) |
---|
278 | return -2; |
---|
279 | return 0; |
---|
280 | } |
---|