1 /*
2  * Copyright 2020, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <lk/compiler.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 __BEGIN_CDECLS
24 
25 /*
26  * Maximum number of instances that can be created by the secure fb server
27  * implementation.
28  */
29 #define SECURE_FB_MAX_INST 4
30 
31 /*
32  * The name of the created secure fb service will be
33  * "SECURE_FB_PORT_NAME"."idx", where idx is 0, 1, ..., SECURE_FB_MAX_INST-1
34  * The number of the instance is a variable depends on how many physical screen
35  * the system has. The index of the instances is consecutive and should start
36  * from 0 to SECURE_FB_MAX_INST-1.
37  */
38 #define SECURE_FB_PORT_NAME "com.android.trusty.secure_fb"
39 
40 /*
41  * The maximum size of the secure_fb port name. The size includes the base name
42  * with ".", "x" and a null terminator where x is the single digit for
43  * identifying instances.
44  */
45 #define SECURE_FB_MAX_PORT_NAME_SIZE (strlen(SECURE_FB_PORT_NAME) + 3)
46 
47 /*
48  * Maximum number of framebuffers that can be allocated by one request to get
49  * framebuffers. Reasonable implementations would use one or two buffers.
50  * However, we also give some room for more exotic implementations.
51  */
52 #define SECURE_FB_MAX_FBS 4
53 
54 /**
55  * enum secure_fb_cmd - command identifiers for secure_fb interface
56  * @SECURE_FB_CMD_RESP_BIT:
57  *      Message is a response.
58  * @SECURE_FB_CMD_REQ_SHIFT:
59  *      Number of bits used by @SECURE_FB_CMD_RESP_BIT.
60  * @SECURE_FB_CMD_GET_FBS:
61  *      Allocate up to %SECURE_FB_MAX_FBS framebuffers and send them to the
62  *      caller using shared memory handles.
63  * @SECURE_FB_CMD_DISPLAY_FB:
64  *      Select one framebuffer as active scan out region or update the screen
65  *      with the selected framebuffer. On the first call of a session, the
66  *      service must finalize the initialization of the secure output pipeline.
67  * @SECURE_FB_CMD_RELEASE:
68  *      Free up all resources and relinquish control over the secure output
69  *      pipeline.
70  */
71 enum secure_fb_cmd {
72     SECURE_FB_CMD_RESP_BIT = 1,
73     SECURE_FB_CMD_REQ_SHIFT = 1,
74     SECURE_FB_CMD_GET_FBS = (1 << SECURE_FB_CMD_REQ_SHIFT),
75     SECURE_FB_CMD_DISPLAY_FB = (2 << SECURE_FB_CMD_REQ_SHIFT),
76     SECURE_FB_CMD_RELEASE = (3 << SECURE_FB_CMD_REQ_SHIFT),
77 };
78 
79 /**
80  * enum secure_fb_pixel_format - idenitifiers for pixel format
81  * @TTUI_PF_INVALID:
82  *      Denotes invalid value.
83  * @TTUI_PF_RGBA8:
84  *      Pixel format with 8 bits per channel such that a pixel can be
85  *      represented as uint32_t 0xAABBGGRR with:
86  *      AA - Alpha channel
87  *      BB - Blue channel
88  *      GG - Green channel
89  *      RR - Red channel
90  */
91 enum secure_fb_pixel_format {
92     TTUI_PF_INVALID = 0,
93     TTUI_PF_RGBA8 = 1,
94 };
95 
96 /*
97  * @brief drawing rotation
98  */
99 /**
100  * enum secure_fb_rotation - secure fb draw rotation
101  * @TTUI_DRAW_ROTATION_0:
102  *      no rotation needed.
103  * @TTUI_DRAW_ROTATION_90:
104  *      90" draw rotation required.
105  * @TTUI_DRAW_ROTATION_180:
106  *      180" draw rotation required.
107  * @TTUI_DRAW_ROTATION_270:
108  *      270" draw rotation required.
109  */
110 enum secure_fb_rotation {
111     TTUI_DRAW_ROTATION_0,
112     TTUI_DRAW_ROTATION_90,
113     TTUI_DRAW_ROTATION_180,
114     TTUI_DRAW_ROTATION_270,
115 };
116 
117 /**
118  * struct secure_fb_info - information about framebuffer's topology
119  * @buffer:        Start of the framebuffer. Unused when used as wire type.
120  * @size:          Size of the framebuffer in bytes.
121  * @pixel_stride:  Distance between the beginning of two adjacent pixels in
122  *                 bytes.
123  * @line_stride:   Distance between the beginning of two lines in bytes.
124  * @width:         Width of the framebuffer in pixels.
125  * @height:        Height of the framebuffer in pixels.
126  * @pixel_format:  Pixel format. (should be TTUI_PF_RGBA8)
127  * @rotation:      Draw rotation. (should be secure_fb_rotation)
128  * @display_index: Index of the display. The first display is 0.
129  */
130 struct secure_fb_info {
131     uint8_t* buffer;
132     uint32_t size;
133     uint32_t pixel_stride;
134     uint32_t line_stride;
135     uint32_t width;
136     uint32_t height;
137     uint32_t pixel_format;
138     uint32_t rotation;
139     uint32_t display_index;
140 };
141 
142 /**
143  * struct secure_fb_desc - framebuffer descriptor
144  * @buffer_id:
145  *      Numeric identifier of the buffer. This id is used to select the next
146  *      active buffer using %SECURE_FB_CMD_DISPLAY_FB.
147  * @handle_index:
148  *      An allocation response may result in up to %SECURE_FB_MAX_FBS allocated
149  *      buffers. These may be transmitted using up to %SECURE_FB_MAX_FBS
150  *      handles. However, multiple buffers may be transmitted with a single
151  *      handle. To this end each buffer has a handle index and offset. The
152  *      handle_index indicates which allocation this buffer is part of, and the
153  *      offset indicates how from the beginning of that allocation this buffer
154  *      starts in bytes.
155  * @offset:
156  *      See handle_index above.
157  * @fb_info:
158  *      Describes buffer's topology.
159  */
160 struct secure_fb_desc {
161     uint32_t buffer_id;
162     uint32_t handle_index;
163     uint32_t offset;
164     struct secure_fb_info fb_info;
165 };
166 
167 /**
168  * struct secure_fb_req - common structure for secure_fb requests.
169  * @cmd: Command identifier - one of &enum secure_fb_cmd.
170  */
171 struct secure_fb_req {
172     uint32_t cmd;
173 };
174 
175 /**
176  * struct secure_fb_resp - common structure for secure_fb responses.
177  * @cmd:    Command identifier - %SECURE_FB_CMD_RESP_BIT or'ed with the command
178  *          identifier of the corresponding request.
179  * @status: Status of requested operation. One of &enum secure_fb_service_error.
180  */
181 struct secure_fb_resp {
182     uint32_t cmd;
183     int32_t status;
184 };
185 
186 /**
187  * struct secure_fb_get_fbs_resp - payload for %SECURE_FB_CMD_GET_FBS response
188  * @num_fbs: Number of framebuffers, at most %SECURE_FB_MAX_FBS.
189  * @fbs:     Descriptors of allocated framebuffers.
190  */
191 struct secure_fb_get_fbs_resp {
192     uint32_t num_fbs;
193     struct secure_fb_desc fbs[];
194 };
195 
196 /**
197  * struct secure_fb_display_fb_req - payload for %SECURE_FB_CMD_DISPLAY_FB
198  *                                   request
199  * @buffer_id: ID of a framebuffer previously allocated with
200  *             %SECURE_FB_CMD_GET_FBS.
201  */
202 struct secure_fb_display_fb_req {
203     uint32_t buffer_id;
204 };
205 
206 enum secure_fb_service_error {
207     SECURE_FB_ERROR_OK = 0,
208     SECURE_FB_ERROR_UNINITIALIZED = -2,
209     SECURE_FB_ERROR_PARAMETERS = -3,
210     SECURE_FB_ERROR_INVALID_REQUEST = -4,
211     SECURE_FB_ERROR_MEMORY_ALLOCATION = -5,
212     SECURE_FB_ERROR_SHARED_MEMORY = -6,
213     SECURE_FB_ERROR_DMA = -7,
214     SECURE_FB_ERROR_OUT_OF_RANGE = -8,
215     SECURE_FB_ERROR_UNSUPPORTED_PANEL = -9,
216     SECURE_FB_ERROR_HARDWARE_ERROR = -10000,
217 };
218 
219 /**
220  * hardware_error() - Is used to propagate driver errors to secure_fb client.
221  * Hardware/Driver errors in the range of (-10000, -1] get mapped to the range
222  * (-20000, -10001]. All other codes get mapped to the generic hardware error
223  * SECURE_FB_ERROR_HARDWARE_ERROR = -10000.
224  *
225  * @e: A hardware error value.
226  *
227  * Return: Recoded hardware error.
228  */
hardware_error(int32_t e)229 static inline int32_t hardware_error(int32_t e) {
230     if (e < 0 && e > SECURE_FB_ERROR_HARDWARE_ERROR) {
231         return SECURE_FB_ERROR_HARDWARE_ERROR + e;
232     } else {
233         return SECURE_FB_ERROR_HARDWARE_ERROR;
234     }
235 }
236 
237 __END_CDECLS
238