blob: dde60295e081b8b895f4356dd3ac00f398fd34b4 [file] [log] [blame]
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001/* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <asm/dma-iommu.h>
15#include <asm/memory.h>
16#include <linux/clk/qcom.h>
17#include <linux/coresight-stm.h>
18#include <linux/delay.h>
19#include <linux/devfreq.h>
20#include <linux/hash.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23#include <linux/iommu.h>
24#include <linux/iopoll.h>
25#include <linux/of.h>
26#include <linux/pm_qos.h>
27#include <linux/regulator/consumer.h>
28#include <linux/slab.h>
29#include <linux/workqueue.h>
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -070030#include <linux/platform_device.h>
31#include <linux/soc/qcom/llcc-qcom.h>
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080032#include <soc/qcom/scm.h>
33#include <soc/qcom/smem.h>
34#include <soc/qcom/subsystem_restart.h>
35#include "hfi_packetization.h"
36#include "msm_vidc_debug.h"
37#include "venus_hfi.h"
38#include "vidc_hfi_io.h"
39
40#define FIRMWARE_SIZE 0X00A00000
41#define REG_ADDR_OFFSET_BITMASK 0x000FFFFF
42#define QDSS_IOVA_START 0x80001000
43
44static struct hal_device_data hal_ctxt;
45
46#define TZBSP_MEM_PROTECT_VIDEO_VAR 0x8
47struct tzbsp_memprot {
48 u32 cp_start;
49 u32 cp_size;
50 u32 cp_nonpixel_start;
51 u32 cp_nonpixel_size;
52};
53
54struct tzbsp_resp {
55 int ret;
56};
57
58#define TZBSP_VIDEO_SET_STATE 0xa
59
60/* Poll interval in uS */
61#define POLL_INTERVAL_US 50
62
63enum tzbsp_video_state {
64 TZBSP_VIDEO_STATE_SUSPEND = 0,
65 TZBSP_VIDEO_STATE_RESUME = 1,
66 TZBSP_VIDEO_STATE_RESTORE_THRESHOLD = 2,
67};
68
69struct tzbsp_video_set_state_req {
70 u32 state; /* should be tzbsp_video_state enum value */
71 u32 spare; /* reserved for future, should be zero */
72};
73
74const struct msm_vidc_gov_data DEFAULT_BUS_VOTE = {
75 .data = NULL,
76 .data_count = 0,
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080077};
78
79const int max_packets = 1000;
80
81static void venus_hfi_pm_handler(struct work_struct *work);
82static DECLARE_DELAYED_WORK(venus_hfi_pm_work, venus_hfi_pm_handler);
83static inline int __resume(struct venus_hfi_device *device);
84static inline int __suspend(struct venus_hfi_device *device);
85static int __disable_regulators(struct venus_hfi_device *device);
86static int __enable_regulators(struct venus_hfi_device *device);
87static inline int __prepare_enable_clks(struct venus_hfi_device *device);
88static inline void __disable_unprepare_clks(struct venus_hfi_device *device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080089static void __flush_debug_queue(struct venus_hfi_device *device, u8 *packet);
90static int __initialize_packetization(struct venus_hfi_device *device);
91static struct hal_session *__get_session(struct venus_hfi_device *device,
92 u32 session_id);
Maheshwar Ajja99422322017-07-07 17:31:15 -070093static bool __is_session_valid(struct venus_hfi_device *device,
94 struct hal_session *session, const char *func);
Praneeth Paladugub71968b2015-08-19 20:47:57 -070095static int __set_clocks(struct venus_hfi_device *device, u32 freq);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080096static int __iface_cmdq_write(struct venus_hfi_device *device,
97 void *pkt);
98static int __load_fw(struct venus_hfi_device *device);
99static void __unload_fw(struct venus_hfi_device *device);
100static int __tzbsp_set_video_state(enum tzbsp_video_state state);
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -0700101static int __enable_subcaches(struct venus_hfi_device *device);
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -0700102static int __set_subcaches(struct venus_hfi_device *device);
103static int __release_subcaches(struct venus_hfi_device *device);
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -0700104static int __disable_subcaches(struct venus_hfi_device *device);
Maheshwar Ajja9ff81a22017-08-05 13:25:55 -0700105static int venus_hfi_noc_error_info(void *dev);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800106
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800107/**
108 * Utility function to enforce some of our assumptions. Spam calls to this
109 * in hotspots in code to double check some of the assumptions that we hold.
110 */
111static inline void __strict_check(struct venus_hfi_device *device)
112{
Surajit Podder29d7b342017-08-29 00:27:50 +0530113 msm_vidc_res_handle_fatal_hw_error(device->res,
114 !mutex_is_locked(&device->lock));
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800115}
116
117static inline void __set_state(struct venus_hfi_device *device,
118 enum venus_hfi_state state)
119{
120 device->state = state;
121}
122
123static inline bool __core_in_valid_state(struct venus_hfi_device *device)
124{
125 return device->state != VENUS_STATE_DEINIT;
126}
127
Shivendra Kakrania143d82b2017-05-18 10:37:56 -0700128static inline bool is_sys_cache_present(struct venus_hfi_device *device)
129{
130 return device->res->sys_cache_present;
131}
132
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800133static void __dump_packet(u8 *packet, enum vidc_msg_prio log_level)
134{
135 u32 c = 0, packet_size = *(u32 *)packet;
136 const int row_size = 32;
137 /*
138 * row must contain enough for 0xdeadbaad * 8 to be converted into
139 * "de ad ba ab " * 8 + '\0'
140 */
141 char row[3 * row_size];
142
143 for (c = 0; c * row_size < packet_size; ++c) {
144 int bytes_to_read = ((c + 1) * row_size > packet_size) ?
145 packet_size % row_size : row_size;
146 hex_dump_to_buffer(packet + c * row_size, bytes_to_read,
147 row_size, 4, row, sizeof(row), false);
148 dprintk(log_level, "%s\n", row);
149 }
150}
151
152static void __sim_modify_cmd_packet(u8 *packet, struct venus_hfi_device *device)
153{
154 struct hfi_cmd_sys_session_init_packet *sys_init;
155 struct hal_session *session = NULL;
156 u8 i;
157 phys_addr_t fw_bias = 0;
158
159 if (!device || !packet) {
160 dprintk(VIDC_ERR, "Invalid Param\n");
161 return;
162 } else if (!device->hal_data->firmware_base
163 || is_iommu_present(device->res)) {
164 return;
165 }
166
167 fw_bias = device->hal_data->firmware_base;
168 sys_init = (struct hfi_cmd_sys_session_init_packet *)packet;
169
170 session = __get_session(device, sys_init->session_id);
171 if (!session) {
172 dprintk(VIDC_DBG, "%s :Invalid session id: %x\n",
173 __func__, sys_init->session_id);
174 return;
175 }
176
177 switch (sys_init->packet_type) {
178 case HFI_CMD_SESSION_EMPTY_BUFFER:
179 if (session->is_decoder) {
180 struct hfi_cmd_session_empty_buffer_compressed_packet
181 *pkt = (struct
182 hfi_cmd_session_empty_buffer_compressed_packet
183 *) packet;
184 pkt->packet_buffer -= fw_bias;
185 } else {
186 struct
187 hfi_cmd_session_empty_buffer_uncompressed_plane0_packet
188 *pkt = (struct
189 hfi_cmd_session_empty_buffer_uncompressed_plane0_packet
190 *) packet;
191 pkt->packet_buffer -= fw_bias;
192 }
193 break;
194 case HFI_CMD_SESSION_FILL_BUFFER:
195 {
196 struct hfi_cmd_session_fill_buffer_packet *pkt =
197 (struct hfi_cmd_session_fill_buffer_packet *)packet;
198 pkt->packet_buffer -= fw_bias;
199 break;
200 }
201 case HFI_CMD_SESSION_SET_BUFFERS:
202 {
203 struct hfi_cmd_session_set_buffers_packet *pkt =
204 (struct hfi_cmd_session_set_buffers_packet *)packet;
205 if (pkt->buffer_type == HFI_BUFFER_OUTPUT ||
206 pkt->buffer_type == HFI_BUFFER_OUTPUT2) {
207 struct hfi_buffer_info *buff;
208
209 buff = (struct hfi_buffer_info *) pkt->rg_buffer_info;
210 buff->buffer_addr -= fw_bias;
211 if (buff->extra_data_addr >= fw_bias)
212 buff->extra_data_addr -= fw_bias;
213 } else {
214 for (i = 0; i < pkt->num_buffers; i++)
215 pkt->rg_buffer_info[i] -= fw_bias;
216 }
217 break;
218 }
219 case HFI_CMD_SESSION_RELEASE_BUFFERS:
220 {
221 struct hfi_cmd_session_release_buffer_packet *pkt =
222 (struct hfi_cmd_session_release_buffer_packet *)packet;
223
224 if (pkt->buffer_type == HFI_BUFFER_OUTPUT ||
225 pkt->buffer_type == HFI_BUFFER_OUTPUT2) {
226 struct hfi_buffer_info *buff;
227
228 buff = (struct hfi_buffer_info *) pkt->rg_buffer_info;
229 buff->buffer_addr -= fw_bias;
230 buff->extra_data_addr -= fw_bias;
231 } else {
232 for (i = 0; i < pkt->num_buffers; i++)
233 pkt->rg_buffer_info[i] -= fw_bias;
234 }
235 break;
236 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800237 default:
238 break;
239 }
240}
241
Surajit Podder29d7b342017-08-29 00:27:50 +0530242static int __acquire_regulator(struct regulator_info *rinfo,
243 struct venus_hfi_device *device)
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800244{
245 int rc = 0;
246
247 if (rinfo->has_hw_power_collapse) {
248 rc = regulator_set_mode(rinfo->regulator,
249 REGULATOR_MODE_NORMAL);
250 if (rc) {
251 /*
252 * This is somewhat fatal, but nothing we can do
253 * about it. We can't disable the regulator w/o
254 * getting it back under s/w control
255 */
256 dprintk(VIDC_WARN,
257 "Failed to acquire regulator control: %s\n",
258 rinfo->name);
259 } else {
260
261 dprintk(VIDC_DBG,
262 "Acquire regulator control from HW: %s\n",
263 rinfo->name);
264
265 }
266 }
267
268 if (!regulator_is_enabled(rinfo->regulator)) {
269 dprintk(VIDC_WARN, "Regulator is not enabled %s\n",
270 rinfo->name);
Surajit Podder29d7b342017-08-29 00:27:50 +0530271 msm_vidc_res_handle_fatal_hw_error(device->res, true);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800272 }
273
274 return rc;
275}
276
277static int __hand_off_regulator(struct regulator_info *rinfo)
278{
279 int rc = 0;
280
281 if (rinfo->has_hw_power_collapse) {
282 rc = regulator_set_mode(rinfo->regulator,
283 REGULATOR_MODE_FAST);
284 if (rc) {
285 dprintk(VIDC_WARN,
286 "Failed to hand off regulator control: %s\n",
287 rinfo->name);
288 } else {
289 dprintk(VIDC_DBG,
290 "Hand off regulator control to HW: %s\n",
291 rinfo->name);
292 }
293 }
294
295 return rc;
296}
297
298static int __hand_off_regulators(struct venus_hfi_device *device)
299{
300 struct regulator_info *rinfo;
301 int rc = 0, c = 0;
302
303 venus_hfi_for_each_regulator(device, rinfo) {
304 rc = __hand_off_regulator(rinfo);
305 /*
306 * If one regulator hand off failed, driver should take
307 * the control for other regulators back.
308 */
309 if (rc)
310 goto err_reg_handoff_failed;
311 c++;
312 }
313
314 return rc;
315err_reg_handoff_failed:
316 venus_hfi_for_each_regulator_reverse_continue(device, rinfo, c)
Surajit Podder29d7b342017-08-29 00:27:50 +0530317 __acquire_regulator(rinfo, device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800318
319 return rc;
320}
321
322static int __write_queue(struct vidc_iface_q_info *qinfo, u8 *packet,
323 bool *rx_req_is_set)
324{
325 struct hfi_queue_header *queue;
326 u32 packet_size_in_words, new_write_idx;
327 u32 empty_space, read_idx;
328 u32 *write_ptr;
329
330 if (!qinfo || !packet) {
331 dprintk(VIDC_ERR, "Invalid Params\n");
332 return -EINVAL;
333 } else if (!qinfo->q_array.align_virtual_addr) {
334 dprintk(VIDC_WARN, "Queues have already been freed\n");
335 return -EINVAL;
336 }
337
338 queue = (struct hfi_queue_header *) qinfo->q_hdr;
339 if (!queue) {
340 dprintk(VIDC_ERR, "queue not present\n");
341 return -ENOENT;
342 }
343
344 if (msm_vidc_debug & VIDC_PKT) {
345 dprintk(VIDC_PKT, "%s: %pK\n", __func__, qinfo);
346 __dump_packet(packet, VIDC_PKT);
347 }
348
349 packet_size_in_words = (*(u32 *)packet) >> 2;
350 if (!packet_size_in_words) {
351 dprintk(VIDC_ERR, "Zero packet size\n");
352 return -ENODATA;
353 }
354
355 read_idx = queue->qhdr_read_idx;
356
357 empty_space = (queue->qhdr_write_idx >= read_idx) ?
358 (queue->qhdr_q_size - (queue->qhdr_write_idx - read_idx)) :
359 (read_idx - queue->qhdr_write_idx);
360 if (empty_space <= packet_size_in_words) {
361 queue->qhdr_tx_req = 1;
362 dprintk(VIDC_ERR, "Insufficient size (%d) to write (%d)\n",
363 empty_space, packet_size_in_words);
364 return -ENOTEMPTY;
365 }
366
367 queue->qhdr_tx_req = 0;
368
369 new_write_idx = (queue->qhdr_write_idx + packet_size_in_words);
370 write_ptr = (u32 *)((qinfo->q_array.align_virtual_addr) +
371 (queue->qhdr_write_idx << 2));
372 if (new_write_idx < queue->qhdr_q_size) {
373 memcpy(write_ptr, packet, packet_size_in_words << 2);
374 } else {
375 new_write_idx -= queue->qhdr_q_size;
376 memcpy(write_ptr, packet, (packet_size_in_words -
377 new_write_idx) << 2);
378 memcpy((void *)qinfo->q_array.align_virtual_addr,
379 packet + ((packet_size_in_words - new_write_idx) << 2),
380 new_write_idx << 2);
381 }
382
383 /*
384 * Memory barrier to make sure packet is written before updating the
385 * write index
386 */
387 mb();
388 queue->qhdr_write_idx = new_write_idx;
389 if (rx_req_is_set)
390 *rx_req_is_set = queue->qhdr_rx_req == 1;
391 /*
392 * Memory barrier to make sure write index is updated before an
393 * interrupt is raised on venus.
394 */
395 mb();
396 return 0;
397}
398
399static void __hal_sim_modify_msg_packet(u8 *packet,
400 struct venus_hfi_device *device)
401{
402 struct hfi_msg_sys_session_init_done_packet *sys_idle;
403 struct hal_session *session = NULL;
404 phys_addr_t fw_bias = 0;
405
406 if (!device || !packet) {
407 dprintk(VIDC_ERR, "Invalid Param\n");
408 return;
409 } else if (!device->hal_data->firmware_base
410 || is_iommu_present(device->res)) {
411 return;
412 }
413
414 fw_bias = device->hal_data->firmware_base;
415 sys_idle = (struct hfi_msg_sys_session_init_done_packet *)packet;
416 session = __get_session(device, sys_idle->session_id);
417
418 if (!session) {
419 dprintk(VIDC_DBG, "%s: Invalid session id: %x\n",
420 __func__, sys_idle->session_id);
421 return;
422 }
423
424 switch (sys_idle->packet_type) {
425 case HFI_MSG_SESSION_FILL_BUFFER_DONE:
426 if (session->is_decoder) {
427 struct
428 hfi_msg_session_fbd_uncompressed_plane0_packet
429 *pkt_uc = (struct
430 hfi_msg_session_fbd_uncompressed_plane0_packet
431 *) packet;
432 pkt_uc->packet_buffer += fw_bias;
433 } else {
434 struct
435 hfi_msg_session_fill_buffer_done_compressed_packet
436 *pkt = (struct
437 hfi_msg_session_fill_buffer_done_compressed_packet
438 *) packet;
439 pkt->packet_buffer += fw_bias;
440 }
441 break;
442 case HFI_MSG_SESSION_EMPTY_BUFFER_DONE:
443 {
444 struct hfi_msg_session_empty_buffer_done_packet *pkt =
445 (struct hfi_msg_session_empty_buffer_done_packet *)packet;
446 pkt->packet_buffer += fw_bias;
447 break;
448 }
449 case HFI_MSG_SESSION_GET_SEQUENCE_HEADER_DONE:
450 {
451 struct
452 hfi_msg_session_get_sequence_header_done_packet
453 *pkt =
454 (struct hfi_msg_session_get_sequence_header_done_packet *)
455 packet;
456 pkt->sequence_header += fw_bias;
457 break;
458 }
459 default:
460 break;
461 }
462}
463
464static int __read_queue(struct vidc_iface_q_info *qinfo, u8 *packet,
465 u32 *pb_tx_req_is_set)
466{
467 struct hfi_queue_header *queue;
468 u32 packet_size_in_words, new_read_idx;
469 u32 *read_ptr;
470 u32 receive_request = 0;
471 int rc = 0;
472
473 if (!qinfo || !packet || !pb_tx_req_is_set) {
474 dprintk(VIDC_ERR, "Invalid Params\n");
475 return -EINVAL;
476 } else if (!qinfo->q_array.align_virtual_addr) {
477 dprintk(VIDC_WARN, "Queues have already been freed\n");
478 return -EINVAL;
479 }
480
481 /*
482 * Memory barrier to make sure data is valid before
483 *reading it
484 */
485 mb();
486 queue = (struct hfi_queue_header *) qinfo->q_hdr;
487
488 if (!queue) {
489 dprintk(VIDC_ERR, "Queue memory is not allocated\n");
490 return -ENOMEM;
491 }
492
493 /*
494 * Do not set receive request for debug queue, if set,
495 * Venus generates interrupt for debug messages even
496 * when there is no response message available.
497 * In general debug queue will not become full as it
498 * is being emptied out for every interrupt from Venus.
499 * Venus will anyway generates interrupt if it is full.
500 */
501 if (queue->qhdr_type & HFI_Q_ID_CTRL_TO_HOST_MSG_Q)
502 receive_request = 1;
503
504 if (queue->qhdr_read_idx == queue->qhdr_write_idx) {
505 queue->qhdr_rx_req = receive_request;
506 *pb_tx_req_is_set = 0;
507 dprintk(VIDC_DBG,
508 "%s queue is empty, rx_req = %u, tx_req = %u, read_idx = %u\n",
509 receive_request ? "message" : "debug",
510 queue->qhdr_rx_req, queue->qhdr_tx_req,
511 queue->qhdr_read_idx);
512 return -ENODATA;
513 }
514
515 read_ptr = (u32 *)((qinfo->q_array.align_virtual_addr) +
516 (queue->qhdr_read_idx << 2));
517 packet_size_in_words = (*read_ptr) >> 2;
518 if (!packet_size_in_words) {
519 dprintk(VIDC_ERR, "Zero packet size\n");
520 return -ENODATA;
521 }
522
523 new_read_idx = queue->qhdr_read_idx + packet_size_in_words;
524 if (((packet_size_in_words << 2) <= VIDC_IFACEQ_VAR_HUGE_PKT_SIZE)
525 && queue->qhdr_read_idx <= queue->qhdr_q_size) {
526 if (new_read_idx < queue->qhdr_q_size) {
527 memcpy(packet, read_ptr,
528 packet_size_in_words << 2);
529 } else {
530 new_read_idx -= queue->qhdr_q_size;
531 memcpy(packet, read_ptr,
532 (packet_size_in_words - new_read_idx) << 2);
533 memcpy(packet + ((packet_size_in_words -
534 new_read_idx) << 2),
535 (u8 *)qinfo->q_array.align_virtual_addr,
536 new_read_idx << 2);
537 }
538 } else {
539 dprintk(VIDC_WARN,
540 "BAD packet received, read_idx: %#x, pkt_size: %d\n",
541 queue->qhdr_read_idx, packet_size_in_words << 2);
542 dprintk(VIDC_WARN, "Dropping this packet\n");
543 new_read_idx = queue->qhdr_write_idx;
544 rc = -ENODATA;
545 }
546
547 queue->qhdr_read_idx = new_read_idx;
548
549 if (queue->qhdr_read_idx != queue->qhdr_write_idx)
550 queue->qhdr_rx_req = 0;
551 else
552 queue->qhdr_rx_req = receive_request;
553
554 *pb_tx_req_is_set = (queue->qhdr_tx_req == 1) ? 1 : 0;
555
Maheshwar Ajja6a990572017-03-31 15:31:57 -0700556 if ((msm_vidc_debug & VIDC_PKT) &&
557 !(queue->qhdr_type & HFI_Q_ID_CTRL_TO_HOST_DEBUG_Q)) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800558 dprintk(VIDC_PKT, "%s: %pK\n", __func__, qinfo);
559 __dump_packet(packet, VIDC_PKT);
560 }
561
562 return rc;
563}
564
565static int __smem_alloc(struct venus_hfi_device *dev,
566 struct vidc_mem_addr *mem, u32 size, u32 align,
567 u32 flags, u32 usage)
568{
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700569 struct msm_smem *alloc = &mem->mem_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800570 int rc = 0;
571
572 if (!dev || !dev->hal_client || !mem || !size) {
573 dprintk(VIDC_ERR, "Invalid Params\n");
574 return -EINVAL;
575 }
576
577 dprintk(VIDC_INFO, "start to alloc size: %d, flags: %d\n", size, flags);
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700578 rc = msm_smem_alloc(dev->hal_client, size, align, flags,
579 usage, 1, alloc);
580 if (rc) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800581 dprintk(VIDC_ERR, "Alloc failed\n");
582 rc = -ENOMEM;
583 goto fail_smem_alloc;
584 }
585
586 dprintk(VIDC_DBG, "__smem_alloc: ptr = %pK, size = %d\n",
587 alloc->kvaddr, size);
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700588 rc = msm_smem_cache_operations(dev->hal_client, alloc->handle, 0,
589 alloc->size, SMEM_CACHE_CLEAN);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800590 if (rc) {
591 dprintk(VIDC_WARN, "Failed to clean cache\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800592 }
593
594 mem->mem_size = alloc->size;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800595 mem->align_virtual_addr = alloc->kvaddr;
596 mem->align_device_addr = alloc->device_addr;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700597
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800598 return rc;
599fail_smem_alloc:
600 return rc;
601}
602
603static void __smem_free(struct venus_hfi_device *dev, struct msm_smem *mem)
604{
605 if (!dev || !mem) {
606 dprintk(VIDC_ERR, "invalid param %pK %pK\n", dev, mem);
607 return;
608 }
609
610 msm_smem_free(dev->hal_client, mem);
611}
612
613static void __write_register(struct venus_hfi_device *device,
614 u32 reg, u32 value)
615{
616 u32 hwiosymaddr = reg;
617 u8 *base_addr;
618
619 if (!device) {
620 dprintk(VIDC_ERR, "Invalid params: %pK\n", device);
621 return;
622 }
623
624 __strict_check(device);
625
626 if (!device->power_enabled) {
627 dprintk(VIDC_WARN,
628 "HFI Write register failed : Power is OFF\n");
Surajit Podder29d7b342017-08-29 00:27:50 +0530629 msm_vidc_res_handle_fatal_hw_error(device->res, true);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800630 return;
631 }
632
633 base_addr = device->hal_data->register_base;
634 dprintk(VIDC_DBG, "Base addr: %pK, written to: %#x, Value: %#x...\n",
635 base_addr, hwiosymaddr, value);
636 base_addr += hwiosymaddr;
637 writel_relaxed(value, base_addr);
638
639 /*
640 * Memory barrier to make sure value is written into the register.
641 */
642 wmb();
643}
644
645static int __read_register(struct venus_hfi_device *device, u32 reg)
646{
647 int rc = 0;
648 u8 *base_addr;
649
650 if (!device) {
651 dprintk(VIDC_ERR, "Invalid params: %pK\n", device);
652 return -EINVAL;
653 }
654
655 __strict_check(device);
656
657 if (!device->power_enabled) {
658 dprintk(VIDC_WARN,
659 "HFI Read register failed : Power is OFF\n");
Surajit Podder29d7b342017-08-29 00:27:50 +0530660 msm_vidc_res_handle_fatal_hw_error(device->res, true);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800661 return -EINVAL;
662 }
663
664 base_addr = device->hal_data->register_base;
665
666 rc = readl_relaxed(base_addr + reg);
667 /*
668 * Memory barrier to make sure value is read correctly from the
669 * register.
670 */
671 rmb();
672 dprintk(VIDC_DBG, "Base addr: %pK, read from: %#x, value: %#x...\n",
673 base_addr, reg, rc);
674
675 return rc;
676}
677
678static void __set_registers(struct venus_hfi_device *device)
679{
680 struct reg_set *reg_set;
681 int i;
682
683 if (!device->res) {
684 dprintk(VIDC_ERR,
685 "device resources null, cannot set registers\n");
686 return;
687 }
688
689 reg_set = &device->res->reg_set;
690 for (i = 0; i < reg_set->count; i++) {
691 __write_register(device, reg_set->reg_tbl[i].reg,
692 reg_set->reg_tbl[i].value);
693 }
694}
695
696/*
697 * The existence of this function is a hack for 8996 (or certain Venus versions)
698 * to overcome a hardware bug. Whenever the GDSCs momentarily power collapse
699 * (after calling __hand_off_regulators()), the values of the threshold
700 * registers (typically programmed by TZ) are incorrectly reset. As a result
701 * reprogram these registers at certain agreed upon points.
702 */
703static void __set_threshold_registers(struct venus_hfi_device *device)
704{
705 u32 version = __read_register(device, VIDC_WRAPPER_HW_VERSION);
706
707 version &= ~GENMASK(15, 0);
708 if (version != (0x3 << 28 | 0x43 << 16))
709 return;
710
711 if (__tzbsp_set_video_state(TZBSP_VIDEO_STATE_RESTORE_THRESHOLD))
712 dprintk(VIDC_ERR, "Failed to restore threshold values\n");
713}
714
715static void __iommu_detach(struct venus_hfi_device *device)
716{
717 struct context_bank_info *cb;
718
719 if (!device || !device->res) {
720 dprintk(VIDC_ERR, "Invalid parameter: %pK\n", device);
721 return;
722 }
723
724 list_for_each_entry(cb, &device->res->context_banks, list) {
725 if (cb->dev)
726 arm_iommu_detach_device(cb->dev);
727 if (cb->mapping)
728 arm_iommu_release_mapping(cb->mapping);
729 }
730}
731
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800732static int __devfreq_target(struct device *devfreq_dev,
733 unsigned long *freq, u32 flags)
734{
735 int rc = 0;
736 uint64_t ab = 0;
737 struct bus_info *bus = NULL, *temp = NULL;
738 struct venus_hfi_device *device = dev_get_drvdata(devfreq_dev);
739
740 venus_hfi_for_each_bus(device, temp) {
741 if (temp->dev == devfreq_dev) {
742 bus = temp;
743 break;
744 }
745 }
746
747 if (!bus) {
748 rc = -EBADHANDLE;
749 goto err_unknown_device;
750 }
751
752 /*
753 * Clamp for all non zero frequencies. This clamp is necessary to stop
754 * devfreq driver from spamming - Couldn't update frequency - logs, if
755 * the scaled ab value is not part of the frequency table.
756 */
757 if (*freq)
758 *freq = clamp_t(typeof(*freq), *freq, bus->range[0],
759 bus->range[1]);
760
761 /* we expect governors to provide values in kBps form, convert to Bps */
762 ab = *freq * 1000;
763 rc = msm_bus_scale_update_bw(bus->client, ab, 0);
764 if (rc) {
765 dprintk(VIDC_ERR, "Failed voting bus %s to ab %llu\n: %d",
766 bus->name, ab, rc);
767 goto err_unknown_device;
768 }
769
770 dprintk(VIDC_PROF, "Voting bus %s to ab %llu\n", bus->name, ab);
771
772 return 0;
773err_unknown_device:
774 return rc;
775}
776
777static int __devfreq_get_status(struct device *devfreq_dev,
778 struct devfreq_dev_status *stat)
779{
780 int rc = 0;
781 struct bus_info *bus = NULL, *temp = NULL;
782 struct venus_hfi_device *device = dev_get_drvdata(devfreq_dev);
783
784 venus_hfi_for_each_bus(device, temp) {
785 if (temp->dev == devfreq_dev) {
786 bus = temp;
787 break;
788 }
789 }
790
791 if (!bus) {
792 rc = -EBADHANDLE;
793 goto err_unknown_device;
794 }
795
796 *stat = (struct devfreq_dev_status) {
797 .private_data = &device->bus_vote,
798 /*
799 * Put in dummy place holder values for upstream govs, our
800 * custom gov only needs .private_data. We should fill this in
801 * properly if we can actually measure busy_time accurately
802 * (which we can't at the moment)
803 */
804 .total_time = 1,
805 .busy_time = 1,
806 .current_frequency = 0,
807 };
808
809err_unknown_device:
810 return rc;
811}
812
813static int __unvote_buses(struct venus_hfi_device *device)
814{
815 int rc = 0;
816 struct bus_info *bus = NULL;
817
Shivendra Kakraniaac170f92017-05-22 13:08:09 -0700818 kfree(device->bus_vote.data);
819 device->bus_vote.data = NULL;
820 device->bus_vote.data_count = 0;
821
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800822 venus_hfi_for_each_bus(device, bus) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800823 unsigned long zero = 0;
824
Shivendra Kakraniaac170f92017-05-22 13:08:09 -0700825 if (!bus->is_prfm_gov_used)
826 rc = devfreq_suspend_device(bus->devfreq);
827 else
828 rc = __devfreq_target(bus->dev, &zero, 0);
829
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800830 if (rc)
831 goto err_unknown_device;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800832 }
833
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800834err_unknown_device:
835 return rc;
836}
837
838static int __vote_buses(struct venus_hfi_device *device,
839 struct vidc_bus_vote_data *data, int num_data)
840{
841 int rc = 0;
842 struct bus_info *bus = NULL;
843 struct vidc_bus_vote_data *new_data = NULL;
844
845 if (!num_data) {
846 dprintk(VIDC_DBG, "No vote data available\n");
847 goto no_data_count;
848 } else if (!data) {
849 dprintk(VIDC_ERR, "Invalid voting data\n");
850 return -EINVAL;
851 }
852
853 new_data = kmemdup(data, num_data * sizeof(*new_data), GFP_KERNEL);
854 if (!new_data) {
855 dprintk(VIDC_ERR, "Can't alloc memory to cache bus votes\n");
856 rc = -ENOMEM;
857 goto err_no_mem;
858 }
859
860no_data_count:
861 kfree(device->bus_vote.data);
862 device->bus_vote.data = new_data;
863 device->bus_vote.data_count = num_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800864
865 venus_hfi_for_each_bus(device, bus) {
866 if (bus && bus->devfreq) {
Shivendra Kakraniaac170f92017-05-22 13:08:09 -0700867 if (!bus->is_prfm_gov_used) {
868 rc = devfreq_resume_device(bus->devfreq);
869 if (rc)
870 goto err_no_mem;
871 } else {
872 bus->devfreq->nb.notifier_call(
873 &bus->devfreq->nb, 0, NULL);
874 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800875 }
876 }
877
878err_no_mem:
879 return rc;
880}
881
882static int venus_hfi_vote_buses(void *dev, struct vidc_bus_vote_data *d, int n)
883{
884 int rc = 0;
885 struct venus_hfi_device *device = dev;
886
887 if (!device)
888 return -EINVAL;
889
890 mutex_lock(&device->lock);
891 rc = __vote_buses(device, d, n);
892 mutex_unlock(&device->lock);
893
894 return rc;
895
896}
897static int __core_set_resource(struct venus_hfi_device *device,
898 struct vidc_resource_hdr *resource_hdr, void *resource_value)
899{
900 struct hfi_cmd_sys_set_resource_packet *pkt;
901 u8 packet[VIDC_IFACEQ_VAR_SMALL_PKT_SIZE];
902 int rc = 0;
903
904 if (!device || !resource_hdr || !resource_value) {
905 dprintk(VIDC_ERR, "set_res: Invalid Params\n");
906 return -EINVAL;
907 }
908
909 pkt = (struct hfi_cmd_sys_set_resource_packet *) packet;
910
911 rc = call_hfi_pkt_op(device, sys_set_resource,
912 pkt, resource_hdr, resource_value);
913 if (rc) {
914 dprintk(VIDC_ERR, "set_res: failed to create packet\n");
915 goto err_create_pkt;
916 }
917
918 rc = __iface_cmdq_write(device, pkt);
919 if (rc)
920 rc = -ENOTEMPTY;
921
922err_create_pkt:
923 return rc;
924}
925
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -0700926static int __core_release_resource(struct venus_hfi_device *device,
927 struct vidc_resource_hdr *resource_hdr)
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800928{
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -0700929 struct hfi_cmd_sys_release_resource_packet *pkt;
930 u8 packet[VIDC_IFACEQ_VAR_SMALL_PKT_SIZE];
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800931 int rc = 0;
932
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -0700933 if (!device || !resource_hdr) {
934 dprintk(VIDC_ERR, "release_res: Invalid Params\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800935 return -EINVAL;
936 }
937
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -0700938 pkt = (struct hfi_cmd_sys_release_resource_packet *) packet;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800939
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -0700940 rc = call_hfi_pkt_op(device, sys_release_resource,
941 pkt, resource_hdr);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800942
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800943 if (rc) {
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -0700944 dprintk(VIDC_ERR, "release_res: failed to create packet\n");
945 goto err_create_pkt;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800946 }
947
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -0700948 rc = __iface_cmdq_write(device, pkt);
949 if (rc)
950 rc = -ENOTEMPTY;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800951
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -0700952err_create_pkt:
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800953 return rc;
954}
955
956static int __tzbsp_set_video_state(enum tzbsp_video_state state)
957{
958 struct tzbsp_video_set_state_req cmd = {0};
959 int tzbsp_rsp = 0;
960 int rc = 0;
961 struct scm_desc desc = {0};
962
963 desc.args[0] = cmd.state = state;
964 desc.args[1] = cmd.spare = 0;
965 desc.arginfo = SCM_ARGS(2);
966
967 if (!is_scm_armv8()) {
968 rc = scm_call(SCM_SVC_BOOT, TZBSP_VIDEO_SET_STATE, &cmd,
969 sizeof(cmd), &tzbsp_rsp, sizeof(tzbsp_rsp));
970 } else {
971 rc = scm_call2(SCM_SIP_FNID(SCM_SVC_BOOT,
972 TZBSP_VIDEO_SET_STATE), &desc);
973 tzbsp_rsp = desc.ret[0];
974 }
975
976 if (rc) {
977 dprintk(VIDC_ERR, "Failed scm_call %d\n", rc);
978 return rc;
979 }
980
981 dprintk(VIDC_DBG, "Set state %d, resp %d\n", state, tzbsp_rsp);
982 if (tzbsp_rsp) {
983 dprintk(VIDC_ERR,
984 "Failed to set video core state to suspend: %d\n",
985 tzbsp_rsp);
986 return -EINVAL;
987 }
988
989 return 0;
990}
991
992static inline int __boot_firmware(struct venus_hfi_device *device)
993{
994 int rc = 0;
Praneeth Paladuguf3a8e5f2016-12-14 11:04:17 -0800995 u32 ctrl_status = 0, count = 0, max_tries = 1000;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800996
997 __write_register(device, VIDC_CTRL_INIT, 0x1);
998 while (!ctrl_status && count < max_tries) {
999 ctrl_status = __read_register(device, VIDC_CPU_CS_SCIACMDARG0);
1000 if ((ctrl_status & 0xFE) == 0x4) {
1001 dprintk(VIDC_ERR, "invalid setting for UC_REGION\n");
1002 break;
1003 }
1004
Praneeth Paladuguf3a8e5f2016-12-14 11:04:17 -08001005 usleep_range(50, 100);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001006 count++;
1007 }
1008
1009 if (count >= max_tries) {
1010 dprintk(VIDC_ERR, "Error booting up vidc firmware\n");
1011 rc = -ETIME;
1012 }
1013 return rc;
1014}
1015
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001016static int venus_hfi_suspend(void *dev)
1017{
1018 int rc = 0;
1019 struct venus_hfi_device *device = (struct venus_hfi_device *) dev;
1020
1021 if (!device) {
1022 dprintk(VIDC_ERR, "%s invalid device\n", __func__);
1023 return -EINVAL;
1024 } else if (!device->res->sw_power_collapsible) {
1025 return -ENOTSUPP;
1026 }
1027
1028 dprintk(VIDC_DBG, "Suspending Venus\n");
Surajit Podder12e9e932017-02-09 18:13:10 +05301029 flush_delayed_work(&venus_hfi_pm_work);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001030
Surajit Podder12e9e932017-02-09 18:13:10 +05301031 mutex_lock(&device->lock);
1032 if (device->power_enabled)
1033 rc = -EBUSY;
1034 mutex_unlock(&device->lock);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001035 return rc;
1036}
1037
1038static int venus_hfi_flush_debug_queue(void *dev)
1039{
1040 int rc = 0;
1041 struct venus_hfi_device *device = (struct venus_hfi_device *) dev;
1042
1043 if (!device) {
1044 dprintk(VIDC_ERR, "%s invalid device\n", __func__);
1045 return -EINVAL;
1046 }
1047
1048 mutex_lock(&device->lock);
1049
Maheshwar Ajjac35d7fc2017-09-01 16:44:46 -07001050 if (!device->power_enabled) {
1051 dprintk(VIDC_WARN, "%s: venus power off\n", __func__);
1052 rc = -EINVAL;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001053 goto exit;
1054 }
1055 __flush_debug_queue(device, NULL);
1056exit:
1057 mutex_unlock(&device->lock);
1058 return rc;
1059}
1060
1061static enum hal_default_properties venus_hfi_get_default_properties(void *dev)
1062{
1063 enum hal_default_properties prop = 0;
1064 struct venus_hfi_device *device = (struct venus_hfi_device *) dev;
1065
1066 if (!device) {
1067 dprintk(VIDC_ERR, "%s invalid device\n", __func__);
1068 return -EINVAL;
1069 }
1070
1071 mutex_lock(&device->lock);
1072
Chinmay Sawarkar2de3f772017-02-07 12:03:44 -08001073 prop = HAL_VIDEO_DYNAMIC_BUF_MODE;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001074
1075 mutex_unlock(&device->lock);
1076 return prop;
1077}
1078
Praneeth Paladugub71968b2015-08-19 20:47:57 -07001079static int __set_clocks(struct venus_hfi_device *device, u32 freq)
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001080{
1081 struct clock_info *cl;
Praneeth Paladugub71968b2015-08-19 20:47:57 -07001082 int rc = 0;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001083
1084 venus_hfi_for_each_clock(device, cl) {
Praneeth Paladugub71968b2015-08-19 20:47:57 -07001085 if (cl->has_scaling) {/* has_scaling */
1086 device->clk_freq = freq;
1087 rc = clk_set_rate(cl->clk, freq);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001088 if (rc) {
1089 dprintk(VIDC_ERR,
Praneeth Paladugub71968b2015-08-19 20:47:57 -07001090 "Failed to set clock rate %u %s: %d %s\n",
1091 freq, cl->name, rc, __func__);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001092 return rc;
1093 }
1094
Surajit Podder4c1fa4a2017-08-17 12:51:43 +05301095 trace_msm_vidc_perf_clock_scale(cl->name, freq);
Praneeth Paladugub71968b2015-08-19 20:47:57 -07001096 dprintk(VIDC_PROF, "Scaling clock %s to %u\n",
1097 cl->name, freq);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001098 }
1099 }
1100
1101 return 0;
1102}
1103
Praneeth Paladugub71968b2015-08-19 20:47:57 -07001104static int venus_hfi_scale_clocks(void *dev, u32 freq)
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001105{
1106 int rc = 0;
1107 struct venus_hfi_device *device = dev;
1108
1109 if (!device) {
1110 dprintk(VIDC_ERR, "Invalid args: %pK\n", device);
1111 return -EINVAL;
1112 }
1113
1114 mutex_lock(&device->lock);
1115
1116 if (__resume(device)) {
1117 dprintk(VIDC_ERR, "Resume from power collapse failed\n");
1118 rc = -ENODEV;
1119 goto exit;
1120 }
1121
Praneeth Paladugub71968b2015-08-19 20:47:57 -07001122 rc = __set_clocks(device, freq);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001123exit:
1124 mutex_unlock(&device->lock);
Praneeth Paladugub71968b2015-08-19 20:47:57 -07001125
1126 return rc;
1127}
1128
1129static int __scale_clocks(struct venus_hfi_device *device)
1130{
1131 int rc = 0;
Praneeth Paladugub71968b2015-08-19 20:47:57 -07001132 struct allowed_clock_rates_table *allowed_clks_tbl = NULL;
1133 u32 rate = 0;
1134
Praneeth Paladugub71968b2015-08-19 20:47:57 -07001135 allowed_clks_tbl = device->res->allowed_clks_tbl;
1136
1137 dprintk(VIDC_DBG, "%s: NULL scale data\n", __func__);
1138 rate = device->clk_freq ? device->clk_freq :
1139 allowed_clks_tbl[0].clock_rate;
1140
1141 rc = __set_clocks(device, rate);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001142 return rc;
1143}
1144
1145/* Writes into cmdq without raising an interrupt */
1146static int __iface_cmdq_write_relaxed(struct venus_hfi_device *device,
1147 void *pkt, bool *requires_interrupt)
1148{
1149 struct vidc_iface_q_info *q_info;
1150 struct vidc_hal_cmd_pkt_hdr *cmd_packet;
1151 int result = -E2BIG;
1152
1153 if (!device || !pkt) {
1154 dprintk(VIDC_ERR, "Invalid Params\n");
1155 return -EINVAL;
1156 }
1157
1158 __strict_check(device);
1159
1160 if (!__core_in_valid_state(device)) {
1161 dprintk(VIDC_DBG, "%s - fw not in init state\n", __func__);
1162 result = -EINVAL;
1163 goto err_q_null;
1164 }
1165
1166 cmd_packet = (struct vidc_hal_cmd_pkt_hdr *)pkt;
1167 device->last_packet_type = cmd_packet->packet_type;
1168
1169 q_info = &device->iface_queues[VIDC_IFACEQ_CMDQ_IDX];
1170 if (!q_info) {
1171 dprintk(VIDC_ERR, "cannot write to shared Q's\n");
1172 goto err_q_null;
1173 }
1174
1175 if (!q_info->q_array.align_virtual_addr) {
1176 dprintk(VIDC_ERR, "cannot write to shared CMD Q's\n");
1177 result = -ENODATA;
1178 goto err_q_null;
1179 }
1180
1181 __sim_modify_cmd_packet((u8 *)pkt, device);
1182 if (__resume(device)) {
1183 dprintk(VIDC_ERR, "%s: Power on failed\n", __func__);
1184 goto err_q_write;
1185 }
1186
1187 if (!__write_queue(q_info, (u8 *)pkt, requires_interrupt)) {
1188 if (device->res->sw_power_collapsible) {
1189 cancel_delayed_work(&venus_hfi_pm_work);
1190 if (!queue_delayed_work(device->venus_pm_workq,
1191 &venus_hfi_pm_work,
1192 msecs_to_jiffies(
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -07001193 device->res->msm_vidc_pwr_collapse_delay))) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001194 dprintk(VIDC_DBG,
1195 "PM work already scheduled\n");
1196 }
1197 }
1198
1199 result = 0;
1200 } else {
1201 dprintk(VIDC_ERR, "__iface_cmdq_write: queue full\n");
1202 }
1203
1204err_q_write:
1205err_q_null:
1206 return result;
1207}
1208
1209static int __iface_cmdq_write(struct venus_hfi_device *device, void *pkt)
1210{
1211 bool needs_interrupt = false;
1212 int rc = __iface_cmdq_write_relaxed(device, pkt, &needs_interrupt);
1213
1214 if (!rc && needs_interrupt) {
1215 /* Consumer of cmdq prefers that we raise an interrupt */
1216 rc = 0;
1217 __write_register(device, VIDC_CPU_IC_SOFTINT,
1218 1 << VIDC_CPU_IC_SOFTINT_H2A_SHFT);
1219 }
1220
1221 return rc;
1222}
1223
1224static int __iface_msgq_read(struct venus_hfi_device *device, void *pkt)
1225{
1226 u32 tx_req_is_set = 0;
1227 int rc = 0;
1228 struct vidc_iface_q_info *q_info;
1229
1230 if (!pkt) {
1231 dprintk(VIDC_ERR, "Invalid Params\n");
1232 return -EINVAL;
1233 }
1234
1235 __strict_check(device);
1236
1237 if (!__core_in_valid_state(device)) {
1238 dprintk(VIDC_DBG, "%s - fw not in init state\n", __func__);
1239 rc = -EINVAL;
1240 goto read_error_null;
1241 }
1242
1243 if (device->iface_queues[VIDC_IFACEQ_MSGQ_IDX].
1244 q_array.align_virtual_addr == 0) {
1245 dprintk(VIDC_ERR, "cannot read from shared MSG Q's\n");
1246 rc = -ENODATA;
1247 goto read_error_null;
1248 }
1249
1250 q_info = &device->iface_queues[VIDC_IFACEQ_MSGQ_IDX];
1251 if (!__read_queue(q_info, (u8 *)pkt, &tx_req_is_set)) {
1252 __hal_sim_modify_msg_packet((u8 *)pkt, device);
1253 if (tx_req_is_set)
1254 __write_register(device, VIDC_CPU_IC_SOFTINT,
1255 1 << VIDC_CPU_IC_SOFTINT_H2A_SHFT);
1256 rc = 0;
1257 } else
1258 rc = -ENODATA;
1259
1260read_error_null:
1261 return rc;
1262}
1263
1264static int __iface_dbgq_read(struct venus_hfi_device *device, void *pkt)
1265{
1266 u32 tx_req_is_set = 0;
1267 int rc = 0;
1268 struct vidc_iface_q_info *q_info;
1269
1270 if (!pkt) {
1271 dprintk(VIDC_ERR, "Invalid Params\n");
1272 return -EINVAL;
1273 }
1274
1275 __strict_check(device);
1276
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001277 if (device->iface_queues[VIDC_IFACEQ_DBGQ_IDX].
1278 q_array.align_virtual_addr == 0) {
1279 dprintk(VIDC_ERR, "cannot read from shared DBG Q's\n");
1280 rc = -ENODATA;
1281 goto dbg_error_null;
1282 }
1283
1284 q_info = &device->iface_queues[VIDC_IFACEQ_DBGQ_IDX];
1285 if (!__read_queue(q_info, (u8 *)pkt, &tx_req_is_set)) {
1286 if (tx_req_is_set)
1287 __write_register(device, VIDC_CPU_IC_SOFTINT,
1288 1 << VIDC_CPU_IC_SOFTINT_H2A_SHFT);
1289 rc = 0;
1290 } else
1291 rc = -ENODATA;
1292
1293dbg_error_null:
1294 return rc;
1295}
1296
1297static void __set_queue_hdr_defaults(struct hfi_queue_header *q_hdr)
1298{
1299 q_hdr->qhdr_status = 0x1;
1300 q_hdr->qhdr_type = VIDC_IFACEQ_DFLT_QHDR;
1301 q_hdr->qhdr_q_size = VIDC_IFACEQ_QUEUE_SIZE / 4;
1302 q_hdr->qhdr_pkt_size = 0;
1303 q_hdr->qhdr_rx_wm = 0x1;
1304 q_hdr->qhdr_tx_wm = 0x1;
1305 q_hdr->qhdr_rx_req = 0x1;
1306 q_hdr->qhdr_tx_req = 0x0;
1307 q_hdr->qhdr_rx_irq_status = 0x0;
1308 q_hdr->qhdr_tx_irq_status = 0x0;
1309 q_hdr->qhdr_read_idx = 0x0;
1310 q_hdr->qhdr_write_idx = 0x0;
1311}
1312
1313static void __interface_queues_release(struct venus_hfi_device *device)
1314{
1315 int i;
1316 struct hfi_mem_map_table *qdss;
1317 struct hfi_mem_map *mem_map;
1318 int num_entries = device->res->qdss_addr_set.count;
1319 unsigned long mem_map_table_base_addr;
1320 struct context_bank_info *cb;
1321
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07001322 if (device->qdss.align_virtual_addr) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001323 qdss = (struct hfi_mem_map_table *)
1324 device->qdss.align_virtual_addr;
1325 qdss->mem_map_num_entries = num_entries;
1326 mem_map_table_base_addr =
1327 device->qdss.align_device_addr +
1328 sizeof(struct hfi_mem_map_table);
1329 qdss->mem_map_table_base_addr =
1330 (u32)mem_map_table_base_addr;
1331 if ((unsigned long)qdss->mem_map_table_base_addr !=
1332 mem_map_table_base_addr) {
1333 dprintk(VIDC_ERR,
1334 "Invalid mem_map_table_base_addr %#lx",
1335 mem_map_table_base_addr);
1336 }
1337
1338 mem_map = (struct hfi_mem_map *)(qdss + 1);
1339 cb = msm_smem_get_context_bank(device->hal_client,
1340 false, HAL_BUFFER_INTERNAL_CMD_QUEUE);
1341
1342 for (i = 0; cb && i < num_entries; i++) {
1343 iommu_unmap(cb->mapping->domain,
1344 mem_map[i].virtual_addr,
1345 mem_map[i].size);
1346 }
1347
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07001348 __smem_free(device, &device->qdss.mem_data);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001349 }
1350
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07001351 __smem_free(device, &device->iface_q_table.mem_data);
1352 __smem_free(device, &device->sfr.mem_data);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001353
1354 for (i = 0; i < VIDC_IFACEQ_NUMQ; i++) {
1355 device->iface_queues[i].q_hdr = NULL;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001356 device->iface_queues[i].q_array.align_virtual_addr = NULL;
1357 device->iface_queues[i].q_array.align_device_addr = 0;
1358 }
1359
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001360 device->iface_q_table.align_virtual_addr = NULL;
1361 device->iface_q_table.align_device_addr = 0;
1362
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001363 device->qdss.align_virtual_addr = NULL;
1364 device->qdss.align_device_addr = 0;
1365
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001366 device->sfr.align_virtual_addr = NULL;
1367 device->sfr.align_device_addr = 0;
1368
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001369 device->mem_addr.align_virtual_addr = NULL;
1370 device->mem_addr.align_device_addr = 0;
1371
1372 msm_smem_delete_client(device->hal_client);
1373 device->hal_client = NULL;
1374}
1375
1376static int __get_qdss_iommu_virtual_addr(struct venus_hfi_device *dev,
1377 struct hfi_mem_map *mem_map, struct dma_iommu_mapping *mapping)
1378{
1379 int i;
1380 int rc = 0;
1381 dma_addr_t iova = QDSS_IOVA_START;
1382 int num_entries = dev->res->qdss_addr_set.count;
1383 struct addr_range *qdss_addr_tbl = dev->res->qdss_addr_set.addr_tbl;
1384
1385 if (!num_entries)
1386 return -ENODATA;
1387
1388 for (i = 0; i < num_entries; i++) {
1389 if (mapping) {
1390 rc = iommu_map(mapping->domain, iova,
1391 qdss_addr_tbl[i].start,
1392 qdss_addr_tbl[i].size,
1393 IOMMU_READ | IOMMU_WRITE);
1394
1395 if (rc) {
1396 dprintk(VIDC_ERR,
1397 "IOMMU QDSS mapping failed for addr %#x\n",
1398 qdss_addr_tbl[i].start);
1399 rc = -ENOMEM;
1400 break;
1401 }
1402 } else {
1403 iova = qdss_addr_tbl[i].start;
1404 }
1405
1406 mem_map[i].virtual_addr = (u32)iova;
1407 mem_map[i].physical_addr = qdss_addr_tbl[i].start;
1408 mem_map[i].size = qdss_addr_tbl[i].size;
1409 mem_map[i].attr = 0x0;
1410
1411 iova += mem_map[i].size;
1412 }
1413
1414 if (i < num_entries) {
1415 dprintk(VIDC_ERR,
1416 "QDSS mapping failed, Freeing other entries %d\n", i);
1417
1418 for (--i; mapping && i >= 0; i--) {
1419 iommu_unmap(mapping->domain,
1420 mem_map[i].virtual_addr,
1421 mem_map[i].size);
1422 }
1423 }
1424
1425 return rc;
1426}
1427
1428static void __setup_ucregion_memory_map(struct venus_hfi_device *device)
1429{
1430 __write_register(device, VIDC_UC_REGION_ADDR,
1431 (u32)device->iface_q_table.align_device_addr);
1432 __write_register(device, VIDC_UC_REGION_SIZE, SHARED_QSIZE);
1433 __write_register(device, VIDC_CPU_CS_SCIACMDARG2,
1434 (u32)device->iface_q_table.align_device_addr);
1435 __write_register(device, VIDC_CPU_CS_SCIACMDARG1, 0x01);
1436 if (device->sfr.align_device_addr)
1437 __write_register(device, VIDC_SFR_ADDR,
1438 (u32)device->sfr.align_device_addr);
1439 if (device->qdss.align_device_addr)
1440 __write_register(device, VIDC_MMAP_ADDR,
1441 (u32)device->qdss.align_device_addr);
1442}
1443
1444static int __interface_queues_init(struct venus_hfi_device *dev)
1445{
1446 struct hfi_queue_table_header *q_tbl_hdr;
1447 struct hfi_queue_header *q_hdr;
1448 u32 i;
1449 int rc = 0;
1450 struct hfi_mem_map_table *qdss;
1451 struct hfi_mem_map *mem_map;
1452 struct vidc_iface_q_info *iface_q;
1453 struct hfi_sfr_struct *vsfr;
1454 struct vidc_mem_addr *mem_addr;
1455 int offset = 0;
1456 int num_entries = dev->res->qdss_addr_set.count;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001457 phys_addr_t fw_bias = 0;
1458 size_t q_size;
1459 unsigned long mem_map_table_base_addr;
1460 struct context_bank_info *cb;
1461
1462 q_size = SHARED_QSIZE - ALIGNED_SFR_SIZE - ALIGNED_QDSS_SIZE;
1463 mem_addr = &dev->mem_addr;
1464 if (!is_iommu_present(dev->res))
1465 fw_bias = dev->hal_data->firmware_base;
1466 rc = __smem_alloc(dev, mem_addr, q_size, 1, 0,
1467 HAL_BUFFER_INTERNAL_CMD_QUEUE);
1468 if (rc) {
1469 dprintk(VIDC_ERR, "iface_q_table_alloc_fail\n");
1470 goto fail_alloc_queue;
1471 }
1472
1473 dev->iface_q_table.align_virtual_addr = mem_addr->align_virtual_addr;
1474 dev->iface_q_table.align_device_addr = mem_addr->align_device_addr -
1475 fw_bias;
1476 dev->iface_q_table.mem_size = VIDC_IFACEQ_TABLE_SIZE;
1477 dev->iface_q_table.mem_data = mem_addr->mem_data;
1478 offset += dev->iface_q_table.mem_size;
1479
1480 for (i = 0; i < VIDC_IFACEQ_NUMQ; i++) {
1481 iface_q = &dev->iface_queues[i];
1482 iface_q->q_array.align_device_addr = mem_addr->align_device_addr
1483 + offset - fw_bias;
1484 iface_q->q_array.align_virtual_addr =
1485 mem_addr->align_virtual_addr + offset;
1486 iface_q->q_array.mem_size = VIDC_IFACEQ_QUEUE_SIZE;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001487 offset += iface_q->q_array.mem_size;
1488 iface_q->q_hdr = VIDC_IFACEQ_GET_QHDR_START_ADDR(
1489 dev->iface_q_table.align_virtual_addr, i);
1490 __set_queue_hdr_defaults(iface_q->q_hdr);
1491 }
1492
1493 if ((msm_vidc_fw_debug_mode & HFI_DEBUG_MODE_QDSS) && num_entries) {
1494 rc = __smem_alloc(dev, mem_addr,
1495 ALIGNED_QDSS_SIZE, 1, 0,
1496 HAL_BUFFER_INTERNAL_CMD_QUEUE);
1497 if (rc) {
1498 dprintk(VIDC_WARN,
1499 "qdss_alloc_fail: QDSS messages logging will not work\n");
1500 dev->qdss.align_device_addr = 0;
1501 } else {
1502 dev->qdss.align_device_addr =
1503 mem_addr->align_device_addr - fw_bias;
1504 dev->qdss.align_virtual_addr =
1505 mem_addr->align_virtual_addr;
1506 dev->qdss.mem_size = ALIGNED_QDSS_SIZE;
1507 dev->qdss.mem_data = mem_addr->mem_data;
1508 }
1509 }
1510
1511 rc = __smem_alloc(dev, mem_addr,
1512 ALIGNED_SFR_SIZE, 1, 0,
1513 HAL_BUFFER_INTERNAL_CMD_QUEUE);
1514 if (rc) {
1515 dprintk(VIDC_WARN, "sfr_alloc_fail: SFR not will work\n");
1516 dev->sfr.align_device_addr = 0;
1517 } else {
1518 dev->sfr.align_device_addr = mem_addr->align_device_addr -
1519 fw_bias;
1520 dev->sfr.align_virtual_addr = mem_addr->align_virtual_addr;
1521 dev->sfr.mem_size = ALIGNED_SFR_SIZE;
1522 dev->sfr.mem_data = mem_addr->mem_data;
1523 }
1524
1525 q_tbl_hdr = (struct hfi_queue_table_header *)
1526 dev->iface_q_table.align_virtual_addr;
1527 q_tbl_hdr->qtbl_version = 0;
1528 q_tbl_hdr->device_addr = (void *)dev;
1529 strlcpy(q_tbl_hdr->name, "msm_v4l2_vidc", sizeof(q_tbl_hdr->name));
1530 q_tbl_hdr->qtbl_size = VIDC_IFACEQ_TABLE_SIZE;
1531 q_tbl_hdr->qtbl_qhdr0_offset = sizeof(struct hfi_queue_table_header);
1532 q_tbl_hdr->qtbl_qhdr_size = sizeof(struct hfi_queue_header);
1533 q_tbl_hdr->qtbl_num_q = VIDC_IFACEQ_NUMQ;
1534 q_tbl_hdr->qtbl_num_active_q = VIDC_IFACEQ_NUMQ;
1535
1536 iface_q = &dev->iface_queues[VIDC_IFACEQ_CMDQ_IDX];
1537 q_hdr = iface_q->q_hdr;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07001538 q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001539 q_hdr->qhdr_type |= HFI_Q_ID_HOST_TO_CTRL_CMD_Q;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001540
1541 iface_q = &dev->iface_queues[VIDC_IFACEQ_MSGQ_IDX];
1542 q_hdr = iface_q->q_hdr;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07001543 q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001544 q_hdr->qhdr_type |= HFI_Q_ID_CTRL_TO_HOST_MSG_Q;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001545
1546 iface_q = &dev->iface_queues[VIDC_IFACEQ_DBGQ_IDX];
1547 q_hdr = iface_q->q_hdr;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07001548 q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001549 q_hdr->qhdr_type |= HFI_Q_ID_CTRL_TO_HOST_DEBUG_Q;
1550 /*
1551 * Set receive request to zero on debug queue as there is no
1552 * need of interrupt from video hardware for debug messages
1553 */
1554 q_hdr->qhdr_rx_req = 0;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001555
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07001556 if (dev->qdss.align_virtual_addr) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001557 qdss = (struct hfi_mem_map_table *)dev->qdss.align_virtual_addr;
1558 qdss->mem_map_num_entries = num_entries;
1559 mem_map_table_base_addr = dev->qdss.align_device_addr +
1560 sizeof(struct hfi_mem_map_table);
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07001561 qdss->mem_map_table_base_addr = mem_map_table_base_addr;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001562
1563 mem_map = (struct hfi_mem_map *)(qdss + 1);
1564 cb = msm_smem_get_context_bank(dev->hal_client, false,
1565 HAL_BUFFER_INTERNAL_CMD_QUEUE);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001566 if (!cb) {
1567 dprintk(VIDC_ERR,
1568 "%s: failed to get context bank\n", __func__);
1569 return -EINVAL;
1570 }
1571
1572 rc = __get_qdss_iommu_virtual_addr(dev, mem_map, cb->mapping);
1573 if (rc) {
1574 dprintk(VIDC_ERR,
1575 "IOMMU mapping failed, Freeing qdss memdata\n");
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07001576 __smem_free(dev, &dev->qdss.mem_data);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001577 dev->qdss.align_virtual_addr = NULL;
1578 dev->qdss.align_device_addr = 0;
1579 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001580 }
1581
1582 vsfr = (struct hfi_sfr_struct *) dev->sfr.align_virtual_addr;
1583 vsfr->bufSize = ALIGNED_SFR_SIZE;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001584
1585 __setup_ucregion_memory_map(dev);
1586 return 0;
1587fail_alloc_queue:
1588 return -ENOMEM;
1589}
1590
1591static int __sys_set_debug(struct venus_hfi_device *device, u32 debug)
1592{
1593 u8 packet[VIDC_IFACEQ_VAR_SMALL_PKT_SIZE];
1594 int rc = 0;
1595 struct hfi_cmd_sys_set_property_packet *pkt =
1596 (struct hfi_cmd_sys_set_property_packet *) &packet;
1597
1598 rc = call_hfi_pkt_op(device, sys_debug_config, pkt, debug);
1599 if (rc) {
1600 dprintk(VIDC_WARN,
1601 "Debug mode setting to FW failed\n");
1602 return -ENOTEMPTY;
1603 }
1604
1605 if (__iface_cmdq_write(device, pkt))
1606 return -ENOTEMPTY;
1607 return 0;
1608}
1609
1610static int __sys_set_coverage(struct venus_hfi_device *device, u32 mode)
1611{
1612 u8 packet[VIDC_IFACEQ_VAR_SMALL_PKT_SIZE];
1613 int rc = 0;
1614 struct hfi_cmd_sys_set_property_packet *pkt =
1615 (struct hfi_cmd_sys_set_property_packet *) &packet;
1616
1617 rc = call_hfi_pkt_op(device, sys_coverage_config,
1618 pkt, mode);
1619 if (rc) {
1620 dprintk(VIDC_WARN,
1621 "Coverage mode setting to FW failed\n");
1622 return -ENOTEMPTY;
1623 }
1624
1625 if (__iface_cmdq_write(device, pkt)) {
1626 dprintk(VIDC_WARN, "Failed to send coverage pkt to f/w\n");
1627 return -ENOTEMPTY;
1628 }
1629
1630 return 0;
1631}
1632
1633static int __sys_set_idle_message(struct venus_hfi_device *device,
1634 bool enable)
1635{
1636 u8 packet[VIDC_IFACEQ_VAR_SMALL_PKT_SIZE];
1637 struct hfi_cmd_sys_set_property_packet *pkt =
1638 (struct hfi_cmd_sys_set_property_packet *) &packet;
1639
1640 if (!enable) {
1641 dprintk(VIDC_DBG, "sys_idle_indicator is not enabled\n");
1642 return 0;
1643 }
1644
1645 call_hfi_pkt_op(device, sys_idle_indicator, pkt, enable);
1646 if (__iface_cmdq_write(device, pkt))
1647 return -ENOTEMPTY;
1648 return 0;
1649}
1650
1651static int __sys_set_power_control(struct venus_hfi_device *device,
1652 bool enable)
1653{
1654 struct regulator_info *rinfo;
1655 bool supported = false;
1656 u8 packet[VIDC_IFACEQ_VAR_SMALL_PKT_SIZE];
1657 struct hfi_cmd_sys_set_property_packet *pkt =
1658 (struct hfi_cmd_sys_set_property_packet *) &packet;
1659
1660 venus_hfi_for_each_regulator(device, rinfo) {
1661 if (rinfo->has_hw_power_collapse) {
1662 supported = true;
1663 break;
1664 }
1665 }
1666
1667 if (!supported)
1668 return 0;
1669
1670 call_hfi_pkt_op(device, sys_power_control, pkt, enable);
1671 if (__iface_cmdq_write(device, pkt))
1672 return -ENOTEMPTY;
1673 return 0;
1674}
1675
1676static int venus_hfi_core_init(void *device)
1677{
Maheshwar Ajja99422322017-07-07 17:31:15 -07001678 int rc = 0;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001679 struct hfi_cmd_sys_init_packet pkt;
1680 struct hfi_cmd_sys_get_property_packet version_pkt;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001681 struct venus_hfi_device *dev;
1682
1683 if (!device) {
1684 dprintk(VIDC_ERR, "Invalid device\n");
1685 return -ENODEV;
1686 }
1687
1688 dev = device;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001689
Maheshwar Ajja99422322017-07-07 17:31:15 -07001690 dprintk(VIDC_DBG, "Core initializing\n");
Praneeth Paladugud3a55222017-07-15 14:41:20 -07001691
1692 mutex_lock(&dev->lock);
1693
1694 dev->bus_vote.data =
1695 kzalloc(sizeof(struct vidc_bus_vote_data), GFP_KERNEL);
1696 dev->bus_vote.data_count = 1;
1697 dev->bus_vote.data->power_mode = VIDC_POWER_TURBO;
1698
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001699 rc = __load_fw(dev);
1700 if (rc) {
1701 dprintk(VIDC_ERR, "Failed to load Venus FW\n");
1702 goto err_load_fw;
1703 }
1704
1705 __set_state(dev, VENUS_STATE_INIT);
1706
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001707 if (!dev->hal_client) {
1708 dev->hal_client = msm_smem_new_client(
1709 SMEM_ION, dev->res, MSM_VIDC_UNKNOWN);
1710 if (dev->hal_client == NULL) {
1711 dprintk(VIDC_ERR, "Failed to alloc ION_Client\n");
1712 rc = -ENODEV;
1713 goto err_core_init;
1714 }
1715
1716 dprintk(VIDC_DBG, "Dev_Virt: %pa, Reg_Virt: %pK\n",
1717 &dev->hal_data->firmware_base,
1718 dev->hal_data->register_base);
1719
1720 rc = __interface_queues_init(dev);
1721 if (rc) {
1722 dprintk(VIDC_ERR, "failed to init queues\n");
1723 rc = -ENOMEM;
1724 goto err_core_init;
1725 }
1726 } else {
1727 dprintk(VIDC_ERR, "hal_client exists\n");
1728 rc = -EEXIST;
1729 goto err_core_init;
1730 }
1731
1732 rc = __boot_firmware(dev);
1733 if (rc) {
1734 dprintk(VIDC_ERR, "Failed to start core\n");
1735 rc = -ENODEV;
1736 goto err_core_init;
1737 }
1738
1739 rc = call_hfi_pkt_op(dev, sys_init, &pkt, HFI_VIDEO_ARCH_OX);
1740 if (rc) {
1741 dprintk(VIDC_ERR, "Failed to create sys init pkt\n");
1742 goto err_core_init;
1743 }
1744
1745 if (__iface_cmdq_write(dev, &pkt)) {
1746 rc = -ENOTEMPTY;
1747 goto err_core_init;
1748 }
1749
1750 rc = call_hfi_pkt_op(dev, sys_image_version, &version_pkt);
1751 if (rc || __iface_cmdq_write(dev, &version_pkt))
1752 dprintk(VIDC_WARN, "Failed to send image version pkt to f/w\n");
1753
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07001754 rc = __enable_subcaches(device);
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07001755 if (!rc)
1756 __set_subcaches(device);
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07001757
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001758 if (dev->res->pm_qos_latency_us) {
1759#ifdef CONFIG_SMP
1760 dev->qos.type = PM_QOS_REQ_AFFINE_IRQ;
1761 dev->qos.irq = dev->hal_data->irq;
1762#endif
1763 pm_qos_add_request(&dev->qos, PM_QOS_CPU_DMA_LATENCY,
1764 dev->res->pm_qos_latency_us);
1765 }
Maheshwar Ajja99422322017-07-07 17:31:15 -07001766 dprintk(VIDC_DBG, "Core inited successfully\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001767 mutex_unlock(&dev->lock);
1768 return rc;
1769err_core_init:
1770 __set_state(dev, VENUS_STATE_DEINIT);
1771 __unload_fw(dev);
1772err_load_fw:
Maheshwar Ajja99422322017-07-07 17:31:15 -07001773 dprintk(VIDC_ERR, "Core init failed\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001774 mutex_unlock(&dev->lock);
1775 return rc;
1776}
1777
1778static int venus_hfi_core_release(void *dev)
1779{
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001780 int rc = 0;
Maheshwar Ajja99422322017-07-07 17:31:15 -07001781 struct venus_hfi_device *device = dev;
1782 struct hal_session *session, *next;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001783
1784 if (!device) {
1785 dprintk(VIDC_ERR, "invalid device\n");
1786 return -ENODEV;
1787 }
1788
1789 mutex_lock(&device->lock);
Maheshwar Ajja99422322017-07-07 17:31:15 -07001790 dprintk(VIDC_DBG, "Core releasing\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001791 if (device->res->pm_qos_latency_us &&
1792 pm_qos_request_active(&device->qos))
1793 pm_qos_remove_request(&device->qos);
Maheshwar Ajjae43dd822017-04-25 15:56:53 -07001794
1795 __resume(device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001796 __set_state(device, VENUS_STATE_DEINIT);
1797 __unload_fw(device);
1798
Maheshwar Ajja99422322017-07-07 17:31:15 -07001799 /* unlink all sessions from device */
1800 list_for_each_entry_safe(session, next, &device->sess_head, list)
1801 list_del(&session->list);
1802
1803 dprintk(VIDC_DBG, "Core released successfully\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001804 mutex_unlock(&device->lock);
1805
1806 return rc;
1807}
1808
1809static int __get_q_size(struct venus_hfi_device *dev, unsigned int q_index)
1810{
1811 struct hfi_queue_header *queue;
1812 struct vidc_iface_q_info *q_info;
1813 u32 write_ptr, read_ptr;
1814
1815 if (q_index >= VIDC_IFACEQ_NUMQ) {
1816 dprintk(VIDC_ERR, "Invalid q index: %d\n", q_index);
1817 return -ENOENT;
1818 }
1819
1820 q_info = &dev->iface_queues[q_index];
1821 if (!q_info) {
1822 dprintk(VIDC_ERR, "cannot read shared Q's\n");
1823 return -ENOENT;
1824 }
1825
1826 queue = (struct hfi_queue_header *)q_info->q_hdr;
1827 if (!queue) {
1828 dprintk(VIDC_ERR, "queue not present\n");
1829 return -ENOENT;
1830 }
1831
1832 write_ptr = (u32)queue->qhdr_write_idx;
1833 read_ptr = (u32)queue->qhdr_read_idx;
1834 return read_ptr - write_ptr;
1835}
1836
1837static void __core_clear_interrupt(struct venus_hfi_device *device)
1838{
1839 u32 intr_status = 0;
1840
1841 if (!device) {
1842 dprintk(VIDC_ERR, "%s: NULL device\n", __func__);
1843 return;
1844 }
1845
1846 intr_status = __read_register(device, VIDC_WRAPPER_INTR_STATUS);
1847
1848 if (intr_status & VIDC_WRAPPER_INTR_STATUS_A2H_BMSK ||
1849 intr_status & VIDC_WRAPPER_INTR_STATUS_A2HWD_BMSK ||
1850 intr_status &
1851 VIDC_CPU_CS_SCIACMDARG0_HFI_CTRL_INIT_IDLE_MSG_BMSK) {
1852 device->intr_status |= intr_status;
1853 device->reg_count++;
1854 dprintk(VIDC_DBG,
1855 "INTERRUPT for device: %pK: times: %d interrupt_status: %d\n",
1856 device, device->reg_count, intr_status);
1857 } else {
1858 device->spur_count++;
1859 dprintk(VIDC_INFO,
1860 "SPURIOUS_INTR for device: %pK: times: %d interrupt_status: %d\n",
1861 device, device->spur_count, intr_status);
1862 }
1863
1864 __write_register(device, VIDC_CPU_CS_A2HSOFTINTCLR, 1);
1865 __write_register(device, VIDC_WRAPPER_INTR_CLEAR, intr_status);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001866}
1867
1868static int venus_hfi_core_ping(void *device)
1869{
1870 struct hfi_cmd_sys_ping_packet pkt;
1871 int rc = 0;
1872 struct venus_hfi_device *dev;
1873
1874 if (!device) {
1875 dprintk(VIDC_ERR, "invalid device\n");
1876 return -ENODEV;
1877 }
1878
1879 dev = device;
1880 mutex_lock(&dev->lock);
1881
1882 rc = call_hfi_pkt_op(dev, sys_ping, &pkt);
1883 if (rc) {
1884 dprintk(VIDC_ERR, "core_ping: failed to create packet\n");
1885 goto err_create_pkt;
1886 }
1887
1888 if (__iface_cmdq_write(dev, &pkt))
1889 rc = -ENOTEMPTY;
1890
1891err_create_pkt:
1892 mutex_unlock(&dev->lock);
1893 return rc;
1894}
1895
1896static int venus_hfi_core_trigger_ssr(void *device,
1897 enum hal_ssr_trigger_type type)
1898{
1899 struct hfi_cmd_sys_test_ssr_packet pkt;
1900 int rc = 0;
1901 struct venus_hfi_device *dev;
1902
1903 if (!device) {
1904 dprintk(VIDC_ERR, "invalid device\n");
1905 return -ENODEV;
1906 }
1907
1908 dev = device;
1909 mutex_lock(&dev->lock);
1910
1911 rc = call_hfi_pkt_op(dev, ssr_cmd, type, &pkt);
1912 if (rc) {
1913 dprintk(VIDC_ERR, "core_ping: failed to create packet\n");
1914 goto err_create_pkt;
1915 }
1916
1917 if (__iface_cmdq_write(dev, &pkt))
1918 rc = -ENOTEMPTY;
1919
1920err_create_pkt:
1921 mutex_unlock(&dev->lock);
1922 return rc;
1923}
1924
1925static int venus_hfi_session_set_property(void *sess,
1926 enum hal_property ptype, void *pdata)
1927{
1928 u8 packet[VIDC_IFACEQ_VAR_LARGE_PKT_SIZE];
1929 struct hfi_cmd_session_set_property_packet *pkt =
1930 (struct hfi_cmd_session_set_property_packet *) &packet;
1931 struct hal_session *session = sess;
1932 struct venus_hfi_device *device;
1933 int rc = 0;
1934
1935 if (!session || !session->device || !pdata) {
1936 dprintk(VIDC_ERR, "Invalid Params\n");
1937 return -EINVAL;
1938 }
1939
1940 device = session->device;
1941 mutex_lock(&device->lock);
1942
1943 dprintk(VIDC_INFO, "in set_prop,with prop id: %#x\n", ptype);
Maheshwar Ajja99422322017-07-07 17:31:15 -07001944 if (!__is_session_valid(device, session, __func__)) {
1945 rc = -EINVAL;
1946 goto err_set_prop;
1947 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001948
1949 rc = call_hfi_pkt_op(device, session_set_property,
1950 pkt, session, ptype, pdata);
1951
1952 if (rc == -ENOTSUPP) {
1953 dprintk(VIDC_DBG,
1954 "set property: unsupported prop id: %#x\n", ptype);
1955 rc = 0;
1956 goto err_set_prop;
1957 } else if (rc) {
1958 dprintk(VIDC_ERR, "set property: failed to create packet\n");
1959 rc = -EINVAL;
1960 goto err_set_prop;
1961 }
1962
1963 if (__iface_cmdq_write(session->device, pkt)) {
1964 rc = -ENOTEMPTY;
1965 goto err_set_prop;
1966 }
1967
1968err_set_prop:
1969 mutex_unlock(&device->lock);
1970 return rc;
1971}
1972
1973static int venus_hfi_session_get_property(void *sess,
1974 enum hal_property ptype)
1975{
1976 struct hfi_cmd_session_get_property_packet pkt = {0};
1977 struct hal_session *session = sess;
1978 int rc = 0;
1979 struct venus_hfi_device *device;
1980
1981 if (!session || !session->device) {
1982 dprintk(VIDC_ERR, "Invalid Params\n");
1983 return -EINVAL;
1984 }
1985
1986 device = session->device;
1987 mutex_lock(&device->lock);
1988
1989 dprintk(VIDC_INFO, "%s: property id: %d\n", __func__, ptype);
Maheshwar Ajja99422322017-07-07 17:31:15 -07001990 if (!__is_session_valid(device, session, __func__)) {
1991 rc = -EINVAL;
1992 goto err_create_pkt;
1993 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001994
1995 rc = call_hfi_pkt_op(device, session_get_property,
1996 &pkt, session, ptype);
1997 if (rc) {
1998 dprintk(VIDC_ERR, "get property profile: pkt failed\n");
1999 goto err_create_pkt;
2000 }
2001
2002 if (__iface_cmdq_write(session->device, &pkt)) {
2003 rc = -ENOTEMPTY;
2004 dprintk(VIDC_ERR, "%s cmdq_write error\n", __func__);
2005 }
2006
2007err_create_pkt:
2008 mutex_unlock(&device->lock);
2009 return rc;
2010}
2011
2012static void __set_default_sys_properties(struct venus_hfi_device *device)
2013{
2014 if (__sys_set_debug(device, msm_vidc_fw_debug))
2015 dprintk(VIDC_WARN, "Setting fw_debug msg ON failed\n");
2016 if (__sys_set_idle_message(device,
2017 device->res->sys_idle_indicator || msm_vidc_sys_idle_indicator))
2018 dprintk(VIDC_WARN, "Setting idle response ON failed\n");
2019 if (__sys_set_power_control(device, msm_vidc_fw_low_power_mode))
2020 dprintk(VIDC_WARN, "Setting h/w power collapse ON failed\n");
2021}
2022
2023static void __session_clean(struct hal_session *session)
2024{
Maheshwar Ajja99422322017-07-07 17:31:15 -07002025 struct hal_session *temp, *next;
2026 struct venus_hfi_device *device;
2027
2028 if (!session || !session->device) {
2029 dprintk(VIDC_WARN, "%s: invalid params\n", __func__);
2030 return;
2031 }
2032 device = session->device;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002033 dprintk(VIDC_DBG, "deleted the session: %pK\n", session);
Maheshwar Ajja99422322017-07-07 17:31:15 -07002034 /*
2035 * session might have been removed from the device list in
2036 * core_release, so check and remove if it is in the list
2037 */
2038 list_for_each_entry_safe(temp, next, &device->sess_head, list) {
2039 if (session == temp) {
2040 list_del(&session->list);
2041 break;
2042 }
2043 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002044 /* Poison the session handle with zeros */
2045 *session = (struct hal_session){ {0} };
2046 kfree(session);
2047}
2048
2049static int venus_hfi_session_clean(void *session)
2050{
2051 struct hal_session *sess_close;
2052 struct venus_hfi_device *device;
2053
2054 if (!session) {
2055 dprintk(VIDC_ERR, "Invalid Params %s\n", __func__);
2056 return -EINVAL;
2057 }
2058
2059 sess_close = session;
2060 device = sess_close->device;
2061
2062 if (!device) {
2063 dprintk(VIDC_ERR, "Invalid device handle %s\n", __func__);
2064 return -EINVAL;
2065 }
2066
2067 mutex_lock(&device->lock);
2068
2069 __session_clean(sess_close);
2070
2071 mutex_unlock(&device->lock);
2072 return 0;
2073}
2074
2075static int venus_hfi_session_init(void *device, void *session_id,
2076 enum hal_domain session_type, enum hal_video_codec codec_type,
2077 void **new_session)
2078{
2079 struct hfi_cmd_sys_session_init_packet pkt;
2080 struct venus_hfi_device *dev;
2081 struct hal_session *s;
2082
2083 if (!device || !new_session) {
2084 dprintk(VIDC_ERR, "%s - invalid input\n", __func__);
2085 return -EINVAL;
2086 }
2087
2088 dev = device;
2089 mutex_lock(&dev->lock);
2090
2091 s = kzalloc(sizeof(struct hal_session), GFP_KERNEL);
2092 if (!s) {
2093 dprintk(VIDC_ERR, "new session fail: Out of memory\n");
2094 goto err_session_init_fail;
2095 }
2096
2097 s->session_id = session_id;
2098 s->is_decoder = (session_type == HAL_VIDEO_DOMAIN_DECODER);
2099 s->device = dev;
2100 s->codec = codec_type;
2101 s->domain = session_type;
2102 dprintk(VIDC_DBG,
2103 "%s: inst %pK, session %pK, codec 0x%x, domain 0x%x\n",
2104 __func__, session_id, s, s->codec, s->domain);
2105
2106 list_add_tail(&s->list, &dev->sess_head);
2107
2108 __set_default_sys_properties(device);
2109
2110 if (call_hfi_pkt_op(dev, session_init, &pkt,
2111 s, session_type, codec_type)) {
2112 dprintk(VIDC_ERR, "session_init: failed to create packet\n");
2113 goto err_session_init_fail;
2114 }
2115
2116 *new_session = s;
2117 if (__iface_cmdq_write(dev, &pkt))
2118 goto err_session_init_fail;
2119
2120 mutex_unlock(&dev->lock);
2121 return 0;
2122
2123err_session_init_fail:
2124 if (s)
2125 __session_clean(s);
2126 *new_session = NULL;
2127 mutex_unlock(&dev->lock);
2128 return -EINVAL;
2129}
2130
2131static int __send_session_cmd(struct hal_session *session, int pkt_type)
2132{
2133 struct vidc_hal_session_cmd_pkt pkt;
2134 int rc = 0;
2135 struct venus_hfi_device *device = session->device;
2136
Maheshwar Ajja99422322017-07-07 17:31:15 -07002137 if (!__is_session_valid(device, session, __func__))
2138 return -EINVAL;
2139
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002140 rc = call_hfi_pkt_op(device, session_cmd,
2141 &pkt, pkt_type, session);
2142 if (rc == -EPERM)
2143 return 0;
2144
2145 if (rc) {
2146 dprintk(VIDC_ERR, "send session cmd: create pkt failed\n");
2147 goto err_create_pkt;
2148 }
2149
2150 if (__iface_cmdq_write(session->device, &pkt))
2151 rc = -ENOTEMPTY;
2152
2153err_create_pkt:
2154 return rc;
2155}
2156
2157static int venus_hfi_session_end(void *session)
2158{
2159 struct hal_session *sess;
2160 struct venus_hfi_device *device;
2161 int rc = 0;
2162
2163 if (!session) {
2164 dprintk(VIDC_ERR, "Invalid Params %s\n", __func__);
2165 return -EINVAL;
2166 }
2167
2168 sess = session;
2169 device = sess->device;
2170
2171 mutex_lock(&device->lock);
2172
2173 if (msm_vidc_fw_coverage) {
2174 if (__sys_set_coverage(sess->device, msm_vidc_fw_coverage))
2175 dprintk(VIDC_WARN, "Fw_coverage msg ON failed\n");
2176 }
2177
2178 rc = __send_session_cmd(session, HFI_CMD_SYS_SESSION_END);
2179
2180 mutex_unlock(&device->lock);
2181
2182 return rc;
2183}
2184
2185static int venus_hfi_session_abort(void *sess)
2186{
2187 struct hal_session *session = sess;
2188 struct venus_hfi_device *device;
2189 int rc = 0;
2190
2191 if (!session || !session->device) {
2192 dprintk(VIDC_ERR, "Invalid Params %s\n", __func__);
2193 return -EINVAL;
2194 }
2195
2196 device = session->device;
2197
2198 mutex_lock(&device->lock);
2199
2200 __flush_debug_queue(device, NULL);
2201 rc = __send_session_cmd(session, HFI_CMD_SYS_SESSION_ABORT);
2202
2203 mutex_unlock(&device->lock);
2204
2205 return rc;
2206}
2207
2208static int venus_hfi_session_set_buffers(void *sess,
2209 struct vidc_buffer_addr_info *buffer_info)
2210{
2211 struct hfi_cmd_session_set_buffers_packet *pkt;
2212 u8 packet[VIDC_IFACEQ_VAR_LARGE_PKT_SIZE];
2213 int rc = 0;
2214 struct hal_session *session = sess;
2215 struct venus_hfi_device *device;
2216
2217 if (!session || !session->device || !buffer_info) {
2218 dprintk(VIDC_ERR, "Invalid Params\n");
2219 return -EINVAL;
2220 }
2221
2222 device = session->device;
2223 mutex_lock(&device->lock);
2224
Maheshwar Ajja99422322017-07-07 17:31:15 -07002225 if (!__is_session_valid(device, session, __func__)) {
2226 rc = -EINVAL;
2227 goto err_create_pkt;
2228 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002229 if (buffer_info->buffer_type == HAL_BUFFER_INPUT) {
2230 /*
2231 * Hardware doesn't care about input buffers being
2232 * published beforehand
2233 */
2234 rc = 0;
2235 goto err_create_pkt;
2236 }
2237
2238 pkt = (struct hfi_cmd_session_set_buffers_packet *)packet;
2239
2240 rc = call_hfi_pkt_op(device, session_set_buffers,
2241 pkt, session, buffer_info);
2242 if (rc) {
2243 dprintk(VIDC_ERR, "set buffers: failed to create packet\n");
2244 goto err_create_pkt;
2245 }
2246
2247 dprintk(VIDC_INFO, "set buffers: %#x\n", buffer_info->buffer_type);
2248 if (__iface_cmdq_write(session->device, pkt))
2249 rc = -ENOTEMPTY;
2250
2251err_create_pkt:
2252 mutex_unlock(&device->lock);
2253 return rc;
2254}
2255
2256static int venus_hfi_session_release_buffers(void *sess,
2257 struct vidc_buffer_addr_info *buffer_info)
2258{
2259 struct hfi_cmd_session_release_buffer_packet *pkt;
2260 u8 packet[VIDC_IFACEQ_VAR_LARGE_PKT_SIZE];
2261 int rc = 0;
2262 struct hal_session *session = sess;
2263 struct venus_hfi_device *device;
2264
2265 if (!session || !session->device || !buffer_info) {
2266 dprintk(VIDC_ERR, "Invalid Params\n");
2267 return -EINVAL;
2268 }
2269
2270 device = session->device;
2271 mutex_lock(&device->lock);
2272
Maheshwar Ajja99422322017-07-07 17:31:15 -07002273 if (!__is_session_valid(device, session, __func__)) {
2274 rc = -EINVAL;
2275 goto err_create_pkt;
2276 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002277 if (buffer_info->buffer_type == HAL_BUFFER_INPUT) {
2278 rc = 0;
2279 goto err_create_pkt;
2280 }
2281
2282 pkt = (struct hfi_cmd_session_release_buffer_packet *) packet;
2283
2284 rc = call_hfi_pkt_op(device, session_release_buffers,
2285 pkt, session, buffer_info);
2286 if (rc) {
2287 dprintk(VIDC_ERR, "release buffers: failed to create packet\n");
2288 goto err_create_pkt;
2289 }
2290
2291 dprintk(VIDC_INFO, "Release buffers: %#x\n", buffer_info->buffer_type);
2292 if (__iface_cmdq_write(session->device, pkt))
2293 rc = -ENOTEMPTY;
2294
2295err_create_pkt:
2296 mutex_unlock(&device->lock);
2297 return rc;
2298}
2299
2300static int venus_hfi_session_load_res(void *session)
2301{
2302 struct hal_session *sess;
2303 struct venus_hfi_device *device;
2304 int rc = 0;
2305
2306 if (!session) {
2307 dprintk(VIDC_ERR, "Invalid Params %s\n", __func__);
2308 return -EINVAL;
2309 }
2310
2311 sess = session;
2312 device = sess->device;
2313
2314 mutex_lock(&device->lock);
2315 rc = __send_session_cmd(sess, HFI_CMD_SESSION_LOAD_RESOURCES);
2316 mutex_unlock(&device->lock);
2317
2318 return rc;
2319}
2320
2321static int venus_hfi_session_release_res(void *session)
2322{
2323 struct hal_session *sess;
2324 struct venus_hfi_device *device;
2325 int rc = 0;
2326
2327 if (!session) {
2328 dprintk(VIDC_ERR, "Invalid Params %s\n", __func__);
2329 return -EINVAL;
2330 }
2331
2332 sess = session;
2333 device = sess->device;
2334
2335 mutex_lock(&device->lock);
2336 rc = __send_session_cmd(sess, HFI_CMD_SESSION_RELEASE_RESOURCES);
2337 mutex_unlock(&device->lock);
2338
2339 return rc;
2340}
2341
2342static int venus_hfi_session_start(void *session)
2343{
2344 struct hal_session *sess;
2345 struct venus_hfi_device *device;
2346 int rc = 0;
2347
2348 if (!session) {
2349 dprintk(VIDC_ERR, "Invalid Params %s\n", __func__);
2350 return -EINVAL;
2351 }
2352
2353 sess = session;
2354 device = sess->device;
2355
2356 mutex_lock(&device->lock);
2357 rc = __send_session_cmd(sess, HFI_CMD_SESSION_START);
2358 mutex_unlock(&device->lock);
2359
2360 return rc;
2361}
2362
2363static int venus_hfi_session_continue(void *session)
2364{
2365 struct hal_session *sess;
2366 struct venus_hfi_device *device;
2367 int rc = 0;
2368
2369 if (!session) {
2370 dprintk(VIDC_ERR, "Invalid Params %s\n", __func__);
2371 return -EINVAL;
2372 }
2373
2374 sess = session;
2375 device = sess->device;
2376
2377 mutex_lock(&device->lock);
2378 rc = __send_session_cmd(sess, HFI_CMD_SESSION_CONTINUE);
2379 mutex_unlock(&device->lock);
2380
2381 return rc;
2382}
2383
2384static int venus_hfi_session_stop(void *session)
2385{
2386 struct hal_session *sess;
2387 struct venus_hfi_device *device;
2388 int rc = 0;
2389
2390 if (!session) {
2391 dprintk(VIDC_ERR, "Invalid Params %s\n", __func__);
2392 return -EINVAL;
2393 }
2394
2395 sess = session;
2396 device = sess->device;
2397
2398 mutex_lock(&device->lock);
2399 rc = __send_session_cmd(sess, HFI_CMD_SESSION_STOP);
2400 mutex_unlock(&device->lock);
2401
2402 return rc;
2403}
2404
2405static int __session_etb(struct hal_session *session,
2406 struct vidc_frame_data *input_frame, bool relaxed)
2407{
2408 int rc = 0;
2409 struct venus_hfi_device *device = session->device;
2410
Maheshwar Ajja99422322017-07-07 17:31:15 -07002411 if (!__is_session_valid(device, session, __func__))
2412 return -EINVAL;
2413
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002414 if (session->is_decoder) {
2415 struct hfi_cmd_session_empty_buffer_compressed_packet pkt;
2416
2417 rc = call_hfi_pkt_op(device, session_etb_decoder,
2418 &pkt, session, input_frame);
2419 if (rc) {
2420 dprintk(VIDC_ERR,
2421 "Session etb decoder: failed to create pkt\n");
2422 goto err_create_pkt;
2423 }
2424
2425 if (!relaxed)
2426 rc = __iface_cmdq_write(session->device, &pkt);
2427 else
2428 rc = __iface_cmdq_write_relaxed(session->device,
2429 &pkt, NULL);
2430 if (rc)
2431 goto err_create_pkt;
2432 } else {
2433 struct hfi_cmd_session_empty_buffer_uncompressed_plane0_packet
2434 pkt;
2435
2436 rc = call_hfi_pkt_op(device, session_etb_encoder,
2437 &pkt, session, input_frame);
2438 if (rc) {
2439 dprintk(VIDC_ERR,
2440 "Session etb encoder: failed to create pkt\n");
2441 goto err_create_pkt;
2442 }
2443
2444 if (!relaxed)
2445 rc = __iface_cmdq_write(session->device, &pkt);
2446 else
2447 rc = __iface_cmdq_write_relaxed(session->device,
2448 &pkt, NULL);
2449 if (rc)
2450 goto err_create_pkt;
2451 }
2452
2453err_create_pkt:
2454 return rc;
2455}
2456
2457static int venus_hfi_session_etb(void *sess,
2458 struct vidc_frame_data *input_frame)
2459{
2460 int rc = 0;
2461 struct hal_session *session = sess;
2462 struct venus_hfi_device *device;
2463
2464 if (!session || !session->device || !input_frame) {
2465 dprintk(VIDC_ERR, "Invalid Params\n");
2466 return -EINVAL;
2467 }
2468
2469 device = session->device;
2470 mutex_lock(&device->lock);
2471 rc = __session_etb(session, input_frame, false);
2472 mutex_unlock(&device->lock);
2473 return rc;
2474}
2475
2476static int __session_ftb(struct hal_session *session,
2477 struct vidc_frame_data *output_frame, bool relaxed)
2478{
2479 int rc = 0;
2480 struct venus_hfi_device *device = session->device;
2481 struct hfi_cmd_session_fill_buffer_packet pkt;
2482
Maheshwar Ajja99422322017-07-07 17:31:15 -07002483 if (!__is_session_valid(device, session, __func__))
2484 return -EINVAL;
2485
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002486 rc = call_hfi_pkt_op(device, session_ftb,
2487 &pkt, session, output_frame);
2488 if (rc) {
2489 dprintk(VIDC_ERR, "Session ftb: failed to create pkt\n");
2490 goto err_create_pkt;
2491 }
2492
2493 if (!relaxed)
2494 rc = __iface_cmdq_write(session->device, &pkt);
2495 else
2496 rc = __iface_cmdq_write_relaxed(session->device,
2497 &pkt, NULL);
2498
2499err_create_pkt:
2500 return rc;
2501}
2502
2503static int venus_hfi_session_ftb(void *sess,
2504 struct vidc_frame_data *output_frame)
2505{
2506 int rc = 0;
2507 struct hal_session *session = sess;
2508 struct venus_hfi_device *device;
2509
2510 if (!session || !session->device || !output_frame) {
2511 dprintk(VIDC_ERR, "Invalid Params\n");
2512 return -EINVAL;
2513 }
2514
2515 device = session->device;
2516 mutex_lock(&device->lock);
2517 rc = __session_ftb(session, output_frame, false);
2518 mutex_unlock(&device->lock);
2519 return rc;
2520}
2521
2522static int venus_hfi_session_process_batch(void *sess,
2523 int num_etbs, struct vidc_frame_data etbs[],
2524 int num_ftbs, struct vidc_frame_data ftbs[])
2525{
2526 int rc = 0, c = 0;
2527 struct hal_session *session = sess;
2528 struct venus_hfi_device *device;
2529 struct hfi_cmd_session_sync_process_packet pkt;
2530
2531 if (!session || !session->device) {
2532 dprintk(VIDC_ERR, "%s: Invalid Params\n", __func__);
2533 return -EINVAL;
2534 }
2535
2536 device = session->device;
2537
2538 mutex_lock(&device->lock);
Maheshwar Ajja99422322017-07-07 17:31:15 -07002539
2540 if (!__is_session_valid(device, session, __func__)) {
2541 rc = -EINVAL;
2542 goto err_etbs_and_ftbs;
2543 }
2544
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002545 for (c = 0; c < num_ftbs; ++c) {
2546 rc = __session_ftb(session, &ftbs[c], true);
2547 if (rc) {
2548 dprintk(VIDC_ERR, "Failed to queue batched ftb: %d\n",
2549 rc);
2550 goto err_etbs_and_ftbs;
2551 }
2552 }
2553
2554 for (c = 0; c < num_etbs; ++c) {
2555 rc = __session_etb(session, &etbs[c], true);
2556 if (rc) {
2557 dprintk(VIDC_ERR, "Failed to queue batched etb: %d\n",
2558 rc);
2559 goto err_etbs_and_ftbs;
2560 }
2561 }
2562
2563 rc = call_hfi_pkt_op(device, session_sync_process, &pkt, session);
2564 if (rc) {
2565 dprintk(VIDC_ERR, "Failed to create sync packet\n");
2566 goto err_etbs_and_ftbs;
2567 }
2568
2569 if (__iface_cmdq_write(session->device, &pkt))
2570 rc = -ENOTEMPTY;
2571
2572err_etbs_and_ftbs:
2573 mutex_unlock(&device->lock);
2574 return rc;
2575}
2576
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002577static int venus_hfi_session_get_buf_req(void *sess)
2578{
2579 struct hfi_cmd_session_get_property_packet pkt;
2580 int rc = 0;
2581 struct hal_session *session = sess;
2582 struct venus_hfi_device *device;
2583
2584 if (!session || !session->device) {
2585 dprintk(VIDC_ERR, "invalid session");
2586 return -ENODEV;
2587 }
2588
2589 device = session->device;
2590 mutex_lock(&device->lock);
2591
Maheshwar Ajja99422322017-07-07 17:31:15 -07002592 if (!__is_session_valid(device, session, __func__)) {
2593 rc = -EINVAL;
2594 goto err_create_pkt;
2595 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002596 rc = call_hfi_pkt_op(device, session_get_buf_req,
2597 &pkt, session);
2598 if (rc) {
2599 dprintk(VIDC_ERR,
2600 "Session get buf req: failed to create pkt\n");
2601 goto err_create_pkt;
2602 }
2603
2604 if (__iface_cmdq_write(session->device, &pkt))
2605 rc = -ENOTEMPTY;
2606err_create_pkt:
2607 mutex_unlock(&device->lock);
2608 return rc;
2609}
2610
2611static int venus_hfi_session_flush(void *sess, enum hal_flush flush_mode)
2612{
2613 struct hfi_cmd_session_flush_packet pkt;
2614 int rc = 0;
2615 struct hal_session *session = sess;
2616 struct venus_hfi_device *device;
2617
2618 if (!session || !session->device) {
2619 dprintk(VIDC_ERR, "invalid session");
2620 return -ENODEV;
2621 }
2622
2623 device = session->device;
2624 mutex_lock(&device->lock);
2625
Maheshwar Ajja99422322017-07-07 17:31:15 -07002626 if (!__is_session_valid(device, session, __func__)) {
2627 rc = -EINVAL;
2628 goto err_create_pkt;
2629 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002630 rc = call_hfi_pkt_op(device, session_flush,
2631 &pkt, session, flush_mode);
2632 if (rc) {
2633 dprintk(VIDC_ERR, "Session flush: failed to create pkt\n");
2634 goto err_create_pkt;
2635 }
2636
2637 if (__iface_cmdq_write(session->device, &pkt))
2638 rc = -ENOTEMPTY;
2639err_create_pkt:
2640 mutex_unlock(&device->lock);
2641 return rc;
2642}
2643
2644static int __check_core_registered(struct hal_device_data core,
2645 phys_addr_t fw_addr, u8 *reg_addr, u32 reg_size,
2646 phys_addr_t irq)
2647{
2648 struct venus_hfi_device *device;
2649 struct list_head *curr, *next;
2650
2651 if (core.dev_count) {
2652 list_for_each_safe(curr, next, &core.dev_head) {
2653 device = list_entry(curr,
2654 struct venus_hfi_device, list);
2655 if (device && device->hal_data->irq == irq &&
2656 (CONTAINS(device->hal_data->
2657 firmware_base,
2658 FIRMWARE_SIZE, fw_addr) ||
2659 CONTAINS(fw_addr, FIRMWARE_SIZE,
2660 device->hal_data->
2661 firmware_base) ||
2662 CONTAINS(device->hal_data->
2663 register_base,
2664 reg_size, reg_addr) ||
2665 CONTAINS(reg_addr, reg_size,
2666 device->hal_data->
2667 register_base) ||
2668 OVERLAPS(device->hal_data->
2669 register_base,
2670 reg_size, reg_addr, reg_size) ||
2671 OVERLAPS(reg_addr, reg_size,
2672 device->hal_data->
2673 register_base, reg_size) ||
2674 OVERLAPS(device->hal_data->
2675 firmware_base,
2676 FIRMWARE_SIZE, fw_addr,
2677 FIRMWARE_SIZE) ||
2678 OVERLAPS(fw_addr, FIRMWARE_SIZE,
2679 device->hal_data->
2680 firmware_base,
2681 FIRMWARE_SIZE))) {
2682 return 0;
2683 }
2684
2685 dprintk(VIDC_INFO, "Device not registered\n");
2686 return -EINVAL;
2687 }
2688 } else {
2689 dprintk(VIDC_INFO, "no device Registered\n");
2690 }
2691
2692 return -EINVAL;
2693}
2694
2695static void __process_fatal_error(
2696 struct venus_hfi_device *device)
2697{
2698 struct msm_vidc_cb_cmd_done cmd_done = {0};
2699
2700 cmd_done.device_id = device->device_id;
2701 device->callback(HAL_SYS_ERROR, &cmd_done);
2702}
2703
2704static int __prepare_pc(struct venus_hfi_device *device)
2705{
2706 int rc = 0;
2707 struct hfi_cmd_sys_pc_prep_packet pkt;
2708
2709 rc = call_hfi_pkt_op(device, sys_pc_prep, &pkt);
2710 if (rc) {
2711 dprintk(VIDC_ERR, "Failed to create sys pc prep pkt\n");
2712 goto err_pc_prep;
2713 }
2714
2715 if (__iface_cmdq_write(device, &pkt))
2716 rc = -ENOTEMPTY;
2717 if (rc)
2718 dprintk(VIDC_ERR, "Failed to prepare venus for power off");
2719err_pc_prep:
2720 return rc;
2721}
2722
2723static void venus_hfi_pm_handler(struct work_struct *work)
2724{
2725 int rc = 0;
2726 u32 wfi_status = 0, idle_status = 0, pc_ready = 0;
2727 int count = 0;
Praneeth Paladuguf3a8e5f2016-12-14 11:04:17 -08002728 const int max_tries = 10;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002729 struct venus_hfi_device *device = list_first_entry(
2730 &hal_ctxt.dev_head, struct venus_hfi_device, list);
2731
2732 if (!device) {
2733 dprintk(VIDC_ERR, "%s: NULL device\n", __func__);
2734 return;
2735 }
2736
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07002737 dprintk(VIDC_PROF,
2738 "Entering venus_hfi_pm_handler\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002739 /*
2740 * It is ok to check this variable outside the lock since
2741 * it is being updated in this context only
2742 */
2743 if (device->skip_pc_count >= VIDC_MAX_PC_SKIP_COUNT) {
2744 dprintk(VIDC_WARN, "Failed to PC for %d times\n",
2745 device->skip_pc_count);
2746 device->skip_pc_count = 0;
2747 __process_fatal_error(device);
2748 return;
2749 }
2750 mutex_lock(&device->lock);
2751 if (!device->power_enabled) {
2752 dprintk(VIDC_DBG, "%s: Power already disabled\n",
2753 __func__);
2754 goto exit;
2755 }
2756
2757 rc = __core_in_valid_state(device);
2758 if (!rc) {
2759 dprintk(VIDC_WARN,
2760 "Core is in bad state, Skipping power collapse\n");
2761 goto skip_power_off;
2762 }
2763 pc_ready = __read_register(device, VIDC_CPU_CS_SCIACMDARG0) &
2764 VIDC_CPU_CS_SCIACMDARG0_HFI_CTRL_PC_READY;
2765 if (!pc_ready) {
2766 wfi_status = __read_register(device,
2767 VIDC_WRAPPER_CPU_STATUS);
2768 idle_status = __read_register(device,
2769 VIDC_CPU_CS_SCIACMDARG0);
Maheshwar Ajja79c2eb82017-04-14 12:23:34 -07002770 if (!(wfi_status & BIT(0))) {
2771 dprintk(VIDC_WARN,
2772 "Skipping PC as wfi_status (%#x) bit not set\n",
2773 wfi_status);
2774 goto skip_power_off;
2775 }
2776 if (device->res->sys_idle_indicator &&
2777 !(idle_status & BIT(30))) {
2778 dprintk(VIDC_WARN,
2779 "Skipping PC as idle_status (%#x) bit not set\n",
2780 idle_status);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002781 goto skip_power_off;
2782 }
2783
2784 rc = __prepare_pc(device);
2785 if (rc) {
2786 dprintk(VIDC_WARN, "Failed PC %d\n", rc);
2787 goto skip_power_off;
2788 }
2789
2790 while (count < max_tries) {
2791 wfi_status = __read_register(device,
2792 VIDC_WRAPPER_CPU_STATUS);
2793 pc_ready = __read_register(device,
2794 VIDC_CPU_CS_SCIACMDARG0);
2795 if ((wfi_status & BIT(0)) && (pc_ready &
2796 VIDC_CPU_CS_SCIACMDARG0_HFI_CTRL_PC_READY))
2797 break;
Praneeth Paladuguf3a8e5f2016-12-14 11:04:17 -08002798 usleep_range(150, 250);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002799 count++;
2800 }
2801
2802 if (count == max_tries) {
2803 dprintk(VIDC_ERR,
2804 "Skip PC. Core is not in right state (%#x, %#x)\n",
2805 wfi_status, pc_ready);
2806 goto skip_power_off;
2807 }
2808 }
2809
Praneeth Paladugu0f4f0222017-08-01 17:33:04 -07002810 __flush_debug_queue(device, device->raw_packet);
2811
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002812 rc = __suspend(device);
2813 if (rc)
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07002814 dprintk(VIDC_ERR, "Failed __suspend\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002815
2816 /* Cancel pending delayed works if any */
2817 cancel_delayed_work(&venus_hfi_pm_work);
2818 device->skip_pc_count = 0;
2819
2820 mutex_unlock(&device->lock);
2821 return;
2822
2823skip_power_off:
2824 device->skip_pc_count++;
2825 dprintk(VIDC_WARN, "Skip PC(%d, %#x, %#x, %#x)\n",
2826 device->skip_pc_count, wfi_status, idle_status, pc_ready);
2827 queue_delayed_work(device->venus_pm_workq,
2828 &venus_hfi_pm_work,
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -07002829 msecs_to_jiffies(
2830 device->res->msm_vidc_pwr_collapse_delay));
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002831exit:
2832 mutex_unlock(&device->lock);
2833}
2834
2835static void __process_sys_error(struct venus_hfi_device *device)
2836{
2837 struct hfi_sfr_struct *vsfr = NULL;
2838
2839 __set_state(device, VENUS_STATE_DEINIT);
2840
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002841 vsfr = (struct hfi_sfr_struct *)device->sfr.align_virtual_addr;
2842 if (vsfr) {
2843 void *p = memchr(vsfr->rg_data, '\0', vsfr->bufSize);
2844 /*
2845 * SFR isn't guaranteed to be NULL terminated
2846 * since SYS_ERROR indicates that Venus is in the
2847 * process of crashing.
2848 */
2849 if (p == NULL)
2850 vsfr->rg_data[vsfr->bufSize - 1] = '\0';
2851
2852 dprintk(VIDC_ERR, "SFR Message from FW: %s\n",
2853 vsfr->rg_data);
2854 }
2855}
2856
2857static void __flush_debug_queue(struct venus_hfi_device *device, u8 *packet)
2858{
2859 bool local_packet = false;
2860 enum vidc_msg_prio log_level = VIDC_FW;
2861
2862 if (!device) {
2863 dprintk(VIDC_ERR, "%s: Invalid params\n", __func__);
2864 return;
2865 }
2866
2867 if (!packet) {
2868 packet = kzalloc(VIDC_IFACEQ_VAR_HUGE_PKT_SIZE, GFP_TEMPORARY);
2869 if (!packet) {
2870 dprintk(VIDC_ERR, "In %s() Fail to allocate mem\n",
2871 __func__);
2872 return;
2873 }
2874
2875 local_packet = true;
2876
2877 /*
2878 * Local packek is used when something FATAL occurred.
2879 * It is good to print these logs by default.
2880 */
2881
2882 log_level = VIDC_ERR;
2883 }
2884
2885 while (!__iface_dbgq_read(device, packet)) {
2886 struct hfi_msg_sys_coverage_packet *pkt =
2887 (struct hfi_msg_sys_coverage_packet *) packet;
2888
2889 if (pkt->packet_type == HFI_MSG_SYS_COV) {
2890 int stm_size = 0;
2891
2892 stm_size = stm_log_inv_ts(0, 0,
2893 pkt->rg_msg_data, pkt->msg_size);
2894 if (stm_size == 0)
2895 dprintk(VIDC_ERR,
2896 "In %s, stm_log returned size of 0\n",
2897 __func__);
2898 } else {
2899 struct hfi_msg_sys_debug_packet *pkt =
2900 (struct hfi_msg_sys_debug_packet *) packet;
2901 dprintk(log_level, "%s", pkt->rg_msg_data);
2902 }
2903 }
2904
2905 if (local_packet)
2906 kfree(packet);
2907}
2908
Maheshwar Ajja99422322017-07-07 17:31:15 -07002909static bool __is_session_valid(struct venus_hfi_device *device,
2910 struct hal_session *session, const char *func)
2911{
2912 struct hal_session *temp = NULL;
2913
2914 if (!device || !session)
2915 goto invalid;
2916
2917 list_for_each_entry(temp, &device->sess_head, list)
2918 if (session == temp)
2919 return true;
2920
2921invalid:
2922 dprintk(VIDC_WARN, "%s: device %pK, invalid session %pK\n",
2923 func, device, session);
2924 return false;
2925}
2926
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08002927static struct hal_session *__get_session(struct venus_hfi_device *device,
2928 u32 session_id)
2929{
2930 struct hal_session *temp = NULL;
2931
2932 list_for_each_entry(temp, &device->sess_head, list) {
2933 if (session_id == hash32_ptr(temp))
2934 return temp;
2935 }
2936
2937 return NULL;
2938}
2939
2940static int __response_handler(struct venus_hfi_device *device)
2941{
2942 struct msm_vidc_cb_info *packets;
2943 int packet_count = 0;
2944 u8 *raw_packet = NULL;
2945 bool requeue_pm_work = true;
2946
2947 if (!device || device->state != VENUS_STATE_INIT)
2948 return 0;
2949
2950 packets = device->response_pkt;
2951
2952 raw_packet = device->raw_packet;
2953
2954 if (!raw_packet || !packets) {
2955 dprintk(VIDC_ERR,
2956 "%s: Invalid args : Res packet = %p, Raw packet = %p\n",
2957 __func__, packets, raw_packet);
2958 return 0;
2959 }
2960
2961 if (device->intr_status & VIDC_WRAPPER_INTR_CLEAR_A2HWD_BMSK) {
2962 struct hfi_sfr_struct *vsfr = (struct hfi_sfr_struct *)
2963 device->sfr.align_virtual_addr;
2964 struct msm_vidc_cb_info info = {
2965 .response_type = HAL_SYS_WATCHDOG_TIMEOUT,
2966 .response.cmd = {
2967 .device_id = device->device_id,
2968 }
2969 };
2970
2971 if (vsfr)
2972 dprintk(VIDC_ERR, "SFR Message from FW: %s\n",
2973 vsfr->rg_data);
2974
2975 dprintk(VIDC_ERR, "Received watchdog timeout\n");
2976 packets[packet_count++] = info;
2977 goto exit;
2978 }
2979
2980 /* Bleed the msg queue dry of packets */
2981 while (!__iface_msgq_read(device, raw_packet)) {
2982 void **session_id = NULL;
2983 struct msm_vidc_cb_info *info = &packets[packet_count++];
2984 struct vidc_hal_sys_init_done sys_init_done = {0};
2985 int rc = 0;
2986
2987 rc = hfi_process_msg_packet(device->device_id,
2988 (struct vidc_hal_msg_pkt_hdr *)raw_packet, info);
2989 if (rc) {
2990 dprintk(VIDC_WARN,
2991 "Corrupt/unknown packet found, discarding\n");
2992 --packet_count;
2993 continue;
2994 }
2995
2996 /* Process the packet types that we're interested in */
2997 switch (info->response_type) {
2998 case HAL_SYS_ERROR:
2999 __process_sys_error(device);
3000 break;
3001 case HAL_SYS_RELEASE_RESOURCE_DONE:
3002 dprintk(VIDC_DBG, "Received SYS_RELEASE_RESOURCE\n");
3003 break;
3004 case HAL_SYS_INIT_DONE:
3005 dprintk(VIDC_DBG, "Received SYS_INIT_DONE\n");
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003006
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003007 sys_init_done.capabilities =
3008 device->sys_init_capabilities;
3009 hfi_process_sys_init_done_prop_read(
3010 (struct hfi_msg_sys_init_done_packet *)
3011 raw_packet, &sys_init_done);
3012 info->response.cmd.data.sys_init_done = sys_init_done;
3013 break;
3014 case HAL_SESSION_LOAD_RESOURCE_DONE:
3015 /*
3016 * Work around for H/W bug, need to re-program these
3017 * registers as part of a handshake agreement with the
3018 * firmware. This strictly only needs to be done for
3019 * decoder secure sessions, but there's no harm in doing
3020 * so for all sessions as it's at worst a NO-OP.
3021 */
3022 __set_threshold_registers(device);
3023 break;
3024 default:
3025 break;
3026 }
3027
3028 /* For session-related packets, validate session */
3029 switch (info->response_type) {
3030 case HAL_SESSION_LOAD_RESOURCE_DONE:
3031 case HAL_SESSION_INIT_DONE:
3032 case HAL_SESSION_END_DONE:
3033 case HAL_SESSION_ABORT_DONE:
3034 case HAL_SESSION_START_DONE:
3035 case HAL_SESSION_STOP_DONE:
3036 case HAL_SESSION_FLUSH_DONE:
3037 case HAL_SESSION_SUSPEND_DONE:
3038 case HAL_SESSION_RESUME_DONE:
3039 case HAL_SESSION_SET_PROP_DONE:
3040 case HAL_SESSION_GET_PROP_DONE:
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003041 case HAL_SESSION_RELEASE_BUFFER_DONE:
3042 case HAL_SESSION_RELEASE_RESOURCE_DONE:
3043 case HAL_SESSION_PROPERTY_INFO:
3044 session_id = &info->response.cmd.session_id;
3045 break;
3046 case HAL_SESSION_ERROR:
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003047 case HAL_SESSION_ETB_DONE:
3048 case HAL_SESSION_FTB_DONE:
3049 session_id = &info->response.data.session_id;
3050 break;
3051 case HAL_SESSION_EVENT_CHANGE:
3052 session_id = &info->response.event.session_id;
3053 break;
3054 case HAL_RESPONSE_UNUSED:
3055 default:
3056 session_id = NULL;
3057 break;
3058 }
3059
3060 /*
3061 * hfi_process_msg_packet provides a session_id that's a hashed
3062 * value of struct hal_session, we need to coerce the hashed
3063 * value back to pointer that we can use. Ideally, hfi_process\
3064 * _msg_packet should take care of this, but it doesn't have
3065 * required information for it
3066 */
3067 if (session_id) {
3068 struct hal_session *session = NULL;
3069
Surajit Podder29d7b342017-08-29 00:27:50 +05303070 if (upper_32_bits((uintptr_t)*session_id) != 0) {
3071 dprintk(VIDC_ERR,
3072 "Upper 32-bits != 0 for sess_id=%pK\n",
3073 *session_id);
3074 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003075 session = __get_session(device,
3076 (u32)(uintptr_t)*session_id);
3077 if (!session) {
3078 dprintk(VIDC_ERR,
3079 "Received a packet (%#x) for an unrecognized session (%pK), discarding\n",
3080 info->response_type,
3081 *session_id);
3082 --packet_count;
3083 continue;
3084 }
3085
3086 *session_id = session->session_id;
3087 }
3088
3089 if (packet_count >= max_packets &&
3090 __get_q_size(device, VIDC_IFACEQ_MSGQ_IDX)) {
3091 dprintk(VIDC_WARN,
3092 "Too many packets in message queue to handle at once, deferring read\n");
3093 break;
3094 }
3095 }
3096
3097 if (requeue_pm_work && device->res->sw_power_collapsible) {
3098 cancel_delayed_work(&venus_hfi_pm_work);
3099 if (!queue_delayed_work(device->venus_pm_workq,
3100 &venus_hfi_pm_work,
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -07003101 msecs_to_jiffies(
3102 device->res->msm_vidc_pwr_collapse_delay))) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003103 dprintk(VIDC_ERR, "PM work already scheduled\n");
3104 }
3105 }
3106
3107exit:
3108 __flush_debug_queue(device, raw_packet);
3109
3110 return packet_count;
3111}
3112
3113static void venus_hfi_core_work_handler(struct work_struct *work)
3114{
3115 struct venus_hfi_device *device = list_first_entry(
3116 &hal_ctxt.dev_head, struct venus_hfi_device, list);
3117 int num_responses = 0, i = 0;
3118 u32 intr_status;
3119
3120 mutex_lock(&device->lock);
3121
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07003122 dprintk(VIDC_DBG, "Handling interrupt\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003123
3124 if (!__core_in_valid_state(device)) {
3125 dprintk(VIDC_DBG, "%s - Core not in init state\n", __func__);
3126 goto err_no_work;
3127 }
3128
3129 if (!device->callback) {
3130 dprintk(VIDC_ERR, "No interrupt callback function: %pK\n",
3131 device);
3132 goto err_no_work;
3133 }
3134
3135 if (__resume(device)) {
3136 dprintk(VIDC_ERR, "%s: Power enable failed\n", __func__);
3137 goto err_no_work;
3138 }
3139
3140 __core_clear_interrupt(device);
3141 num_responses = __response_handler(device);
3142
3143err_no_work:
3144
3145 /* Keep the interrupt status before releasing device lock */
3146 intr_status = device->intr_status;
3147 mutex_unlock(&device->lock);
3148
3149 /*
3150 * Issue the callbacks outside of the locked contex to preserve
3151 * re-entrancy.
3152 */
3153
3154 for (i = 0; !IS_ERR_OR_NULL(device->response_pkt) &&
3155 i < num_responses; ++i) {
3156 struct msm_vidc_cb_info *r = &device->response_pkt[i];
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07003157 dprintk(VIDC_DBG, "Processing response %d of %d, type %d\n",
3158 (i + 1), num_responses, r->response_type);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003159 device->callback(r->response_type, &r->response);
3160 }
3161
3162 /* We need re-enable the irq which was disabled in ISR handler */
3163 if (!(intr_status & VIDC_WRAPPER_INTR_STATUS_A2HWD_BMSK))
3164 enable_irq(device->hal_data->irq);
3165
Maheshwar Ajjac6407c02017-06-09 18:53:20 -07003166 dprintk(VIDC_DBG, "Handling interrupt done\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003167 /*
3168 * XXX: Don't add any code beyond here. Reacquiring locks after release
3169 * it above doesn't guarantee the atomicity that we're aiming for.
3170 */
3171}
3172
3173static DECLARE_WORK(venus_hfi_work, venus_hfi_core_work_handler);
3174
3175static irqreturn_t venus_hfi_isr(int irq, void *dev)
3176{
3177 struct venus_hfi_device *device = dev;
3178
3179 dprintk(VIDC_INFO, "Received an interrupt %d\n", irq);
3180 disable_irq_nosync(irq);
3181 queue_work(device->vidc_workq, &venus_hfi_work);
3182 return IRQ_HANDLED;
3183}
3184
3185static int __init_regs_and_interrupts(struct venus_hfi_device *device,
3186 struct msm_vidc_platform_resources *res)
3187{
3188 struct hal_data *hal = NULL;
3189 int rc = 0;
3190
3191 rc = __check_core_registered(hal_ctxt, res->firmware_base,
3192 (u8 *)(uintptr_t)res->register_base,
3193 res->register_size, res->irq);
3194 if (!rc) {
3195 dprintk(VIDC_ERR, "Core present/Already added\n");
3196 rc = -EEXIST;
3197 goto err_core_init;
3198 }
3199
3200 dprintk(VIDC_DBG, "HAL_DATA will be assigned now\n");
3201 hal = (struct hal_data *)
3202 kzalloc(sizeof(struct hal_data), GFP_KERNEL);
3203 if (!hal) {
3204 dprintk(VIDC_ERR, "Failed to alloc\n");
3205 rc = -ENOMEM;
3206 goto err_core_init;
3207 }
3208
3209 hal->irq = res->irq;
3210 hal->firmware_base = res->firmware_base;
3211 hal->register_base = devm_ioremap_nocache(&res->pdev->dev,
3212 res->register_base, res->register_size);
3213 hal->register_size = res->register_size;
3214 if (!hal->register_base) {
3215 dprintk(VIDC_ERR,
3216 "could not map reg addr %pa of size %d\n",
3217 &res->register_base, res->register_size);
3218 goto error_irq_fail;
3219 }
3220
3221 device->hal_data = hal;
3222 rc = request_irq(res->irq, venus_hfi_isr, IRQF_TRIGGER_HIGH,
3223 "msm_vidc", device);
3224 if (unlikely(rc)) {
3225 dprintk(VIDC_ERR, "() :request_irq failed\n");
3226 goto error_irq_fail;
3227 }
3228
3229 disable_irq_nosync(res->irq);
3230 dprintk(VIDC_INFO,
3231 "firmware_base = %pa, register_base = %pa, register_size = %d\n",
3232 &res->firmware_base, &res->register_base,
3233 res->register_size);
3234 return rc;
3235
3236error_irq_fail:
3237 kfree(hal);
3238err_core_init:
3239 return rc;
3240
3241}
3242
3243static inline void __deinit_clocks(struct venus_hfi_device *device)
3244{
3245 struct clock_info *cl;
3246
3247 device->clk_freq = 0;
3248 venus_hfi_for_each_clock_reverse(device, cl) {
3249 if (cl->clk) {
3250 clk_put(cl->clk);
3251 cl->clk = NULL;
3252 }
3253 }
3254}
3255
3256static inline int __init_clocks(struct venus_hfi_device *device)
3257{
3258 int rc = 0;
3259 struct clock_info *cl = NULL;
3260
3261 if (!device) {
3262 dprintk(VIDC_ERR, "Invalid params: %pK\n", device);
3263 return -EINVAL;
3264 }
3265
3266 venus_hfi_for_each_clock(device, cl) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003267
3268 dprintk(VIDC_DBG, "%s: scalable? %d, count %d\n",
3269 cl->name, cl->has_scaling, cl->count);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003270 }
3271
3272 venus_hfi_for_each_clock(device, cl) {
3273 if (!cl->clk) {
3274 cl->clk = clk_get(&device->res->pdev->dev, cl->name);
3275 if (IS_ERR_OR_NULL(cl->clk)) {
3276 dprintk(VIDC_ERR,
3277 "Failed to get clock: %s\n", cl->name);
3278 rc = PTR_ERR(cl->clk) ?: -EINVAL;
3279 cl->clk = NULL;
3280 goto err_clk_get;
3281 }
3282 }
3283 }
3284 device->clk_freq = 0;
3285 return 0;
3286
3287err_clk_get:
3288 __deinit_clocks(device);
3289 return rc;
3290}
3291
3292
3293static inline void __disable_unprepare_clks(struct venus_hfi_device *device)
3294{
3295 struct clock_info *cl;
Praneeth Paladugu03edb082017-04-17 10:31:14 -07003296 int rc = 0;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003297
3298 if (!device) {
3299 dprintk(VIDC_ERR, "Invalid params: %pK\n", device);
3300 return;
3301 }
3302
3303 venus_hfi_for_each_clock_reverse(device, cl) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003304 dprintk(VIDC_DBG, "Clock: %s disable and unprepare\n",
3305 cl->name);
Praneeth Paladugu03edb082017-04-17 10:31:14 -07003306 rc = clk_set_flags(cl->clk, CLKFLAG_NORETAIN_PERIPH);
3307 if (rc) {
3308 dprintk(VIDC_WARN,
3309 "Failed set flag NORETAIN_PERIPH %s\n",
3310 cl->name);
3311 }
3312 rc = clk_set_flags(cl->clk, CLKFLAG_NORETAIN_MEM);
3313 if (rc) {
3314 dprintk(VIDC_WARN,
3315 "Failed set flag NORETAIN_MEM %s\n",
3316 cl->name);
3317 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003318 clk_disable_unprepare(cl->clk);
3319 }
3320}
3321
3322static inline int __prepare_enable_clks(struct venus_hfi_device *device)
3323{
3324 struct clock_info *cl = NULL, *cl_fail = NULL;
3325 int rc = 0, c = 0;
3326
3327 if (!device) {
3328 dprintk(VIDC_ERR, "Invalid params: %pK\n", device);
3329 return -EINVAL;
3330 }
3331
3332 venus_hfi_for_each_clock(device, cl) {
3333 /*
3334 * For the clocks we control, set the rate prior to preparing
3335 * them. Since we don't really have a load at this point, scale
3336 * it to the lowest frequency possible
3337 */
3338 if (cl->has_scaling)
3339 clk_set_rate(cl->clk, clk_round_rate(cl->clk, 0));
3340
Praneeth Paladugu03edb082017-04-17 10:31:14 -07003341 rc = clk_set_flags(cl->clk, CLKFLAG_RETAIN_PERIPH);
3342 if (rc) {
3343 dprintk(VIDC_WARN,
3344 "Failed set flag RETAIN_PERIPH %s\n",
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003345 cl->name);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003346 }
Praneeth Paladugu03edb082017-04-17 10:31:14 -07003347 rc = clk_set_flags(cl->clk, CLKFLAG_RETAIN_MEM);
3348 if (rc) {
3349 dprintk(VIDC_WARN,
3350 "Failed set flag RETAIN_MEM %s\n",
3351 cl->name);
3352 }
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003353 rc = clk_prepare_enable(cl->clk);
3354 if (rc) {
3355 dprintk(VIDC_ERR, "Failed to enable clocks\n");
3356 cl_fail = cl;
3357 goto fail_clk_enable;
3358 }
3359
3360 c++;
3361 dprintk(VIDC_DBG, "Clock: %s prepared and enabled\n", cl->name);
3362 }
3363
3364 __write_register(device, VIDC_WRAPPER_CLOCK_CONFIG, 0);
3365 __write_register(device, VIDC_WRAPPER_CPU_CLOCK_CONFIG, 0);
3366 return rc;
3367
3368fail_clk_enable:
3369 venus_hfi_for_each_clock_reverse_continue(device, cl, c) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003370 dprintk(VIDC_ERR, "Clock: %s disable and unprepare\n",
3371 cl->name);
3372 clk_disable_unprepare(cl->clk);
3373 }
3374
3375 return rc;
3376}
3377
3378static void __deinit_bus(struct venus_hfi_device *device)
3379{
3380 struct bus_info *bus = NULL;
3381
3382 if (!device)
3383 return;
3384
3385 kfree(device->bus_vote.data);
3386 device->bus_vote = DEFAULT_BUS_VOTE;
3387
3388 venus_hfi_for_each_bus_reverse(device, bus) {
3389 devfreq_remove_device(bus->devfreq);
3390 bus->devfreq = NULL;
3391 dev_set_drvdata(bus->dev, NULL);
3392
3393 msm_bus_scale_unregister(bus->client);
3394 bus->client = NULL;
3395 }
3396}
3397
3398static int __init_bus(struct venus_hfi_device *device)
3399{
3400 struct bus_info *bus = NULL;
3401 int rc = 0;
3402
3403 if (!device)
3404 return -EINVAL;
3405
3406 venus_hfi_for_each_bus(device, bus) {
3407 struct devfreq_dev_profile profile = {
3408 .initial_freq = 0,
3409 .polling_ms = INT_MAX,
3410 .freq_table = NULL,
3411 .max_state = 0,
3412 .target = __devfreq_target,
3413 .get_dev_status = __devfreq_get_status,
3414 .exit = NULL,
3415 };
3416
Shivendra Kakraniad2ba4422017-07-27 16:52:23 -07003417 if (!strcmp(bus->governor, "msm-vidc-llcc")) {
3418 if (msm_vidc_syscache_disable) {
3419 dprintk(VIDC_DBG,
3420 "Skipping LLC bus init %s: %s\n",
3421 bus->name, bus->governor);
3422 continue;
3423 }
3424 }
3425
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003426 /*
3427 * This is stupid, but there's no other easy way to ahold
3428 * of struct bus_info in venus_hfi_devfreq_*()
3429 */
3430 WARN(dev_get_drvdata(bus->dev), "%s's drvdata already set\n",
3431 dev_name(bus->dev));
3432 dev_set_drvdata(bus->dev, device);
3433
3434 bus->client = msm_bus_scale_register(bus->master, bus->slave,
3435 bus->name, false);
3436 if (IS_ERR_OR_NULL(bus->client)) {
3437 rc = PTR_ERR(bus->client) ?: -EBADHANDLE;
3438 dprintk(VIDC_ERR, "Failed to register bus %s: %d\n",
3439 bus->name, rc);
3440 bus->client = NULL;
3441 goto err_add_dev;
3442 }
3443
3444 bus->devfreq_prof = profile;
3445 bus->devfreq = devfreq_add_device(bus->dev,
3446 &bus->devfreq_prof, bus->governor, NULL);
3447 if (IS_ERR_OR_NULL(bus->devfreq)) {
3448 rc = PTR_ERR(bus->devfreq) ?: -EBADHANDLE;
3449 dprintk(VIDC_ERR,
3450 "Failed to add devfreq device for bus %s and governor %s: %d\n",
3451 bus->name, bus->governor, rc);
3452 bus->devfreq = NULL;
3453 goto err_add_dev;
3454 }
3455
3456 /*
3457 * Devfreq starts monitoring immediately, since we are just
3458 * initializing stuff at this point, force it to suspend
3459 */
3460 devfreq_suspend_device(bus->devfreq);
3461 }
3462
3463 device->bus_vote = DEFAULT_BUS_VOTE;
3464 return 0;
3465
3466err_add_dev:
3467 __deinit_bus(device);
3468 return rc;
3469}
3470
3471static void __deinit_regulators(struct venus_hfi_device *device)
3472{
3473 struct regulator_info *rinfo = NULL;
3474
3475 venus_hfi_for_each_regulator_reverse(device, rinfo) {
3476 if (rinfo->regulator) {
3477 regulator_put(rinfo->regulator);
3478 rinfo->regulator = NULL;
3479 }
3480 }
3481}
3482
3483static int __init_regulators(struct venus_hfi_device *device)
3484{
3485 int rc = 0;
3486 struct regulator_info *rinfo = NULL;
3487
3488 venus_hfi_for_each_regulator(device, rinfo) {
3489 rinfo->regulator = regulator_get(&device->res->pdev->dev,
3490 rinfo->name);
3491 if (IS_ERR_OR_NULL(rinfo->regulator)) {
3492 rc = PTR_ERR(rinfo->regulator) ?: -EBADHANDLE;
3493 dprintk(VIDC_ERR, "Failed to get regulator: %s\n",
3494 rinfo->name);
3495 rinfo->regulator = NULL;
3496 goto err_reg_get;
3497 }
3498 }
3499
3500 return 0;
3501
3502err_reg_get:
3503 __deinit_regulators(device);
3504 return rc;
3505}
3506
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003507static void __deinit_subcaches(struct venus_hfi_device *device)
3508{
3509 struct subcache_info *sinfo = NULL;
3510
3511 if (!device) {
3512 dprintk(VIDC_ERR, "deinit_subcaches: invalid device %pK\n",
3513 device);
3514 goto exit;
3515 }
3516
Shivendra Kakrania143d82b2017-05-18 10:37:56 -07003517 if (!is_sys_cache_present(device))
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003518 goto exit;
3519
3520 venus_hfi_for_each_subcache_reverse(device, sinfo) {
3521 if (sinfo->subcache) {
3522 dprintk(VIDC_DBG, "deinit_subcaches: %s\n",
3523 sinfo->name);
3524 llcc_slice_putd(sinfo->subcache);
3525 sinfo->subcache = NULL;
3526 }
3527 }
3528
3529exit:
3530 return;
3531}
3532
3533static int __init_subcaches(struct venus_hfi_device *device)
3534{
3535 int rc = 0;
3536 struct subcache_info *sinfo = NULL;
3537
3538 if (!device) {
3539 dprintk(VIDC_ERR, "init_subcaches: invalid device %pK\n",
3540 device);
3541 return -EINVAL;
3542 }
3543
Shivendra Kakrania143d82b2017-05-18 10:37:56 -07003544 if (!is_sys_cache_present(device))
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003545 return 0;
3546
3547 venus_hfi_for_each_subcache(device, sinfo) {
3548 sinfo->subcache = llcc_slice_getd(&device->res->pdev->dev,
3549 sinfo->name);
3550 if (IS_ERR_OR_NULL(sinfo->subcache)) {
3551 rc = PTR_ERR(sinfo->subcache) ? : -EBADHANDLE;
3552 dprintk(VIDC_ERR,
3553 "init_subcaches: invalid subcache: %s rc %d\n",
3554 sinfo->name, rc);
3555 sinfo->subcache = NULL;
3556 goto err_subcache_get;
3557 }
3558 dprintk(VIDC_DBG, "init_subcaches: %s\n",
3559 sinfo->name);
3560 }
3561
3562 return 0;
3563
3564err_subcache_get:
3565 __deinit_subcaches(device);
3566 return rc;
3567}
3568
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003569static int __init_resources(struct venus_hfi_device *device,
3570 struct msm_vidc_platform_resources *res)
3571{
3572 int rc = 0;
3573
3574 rc = __init_regulators(device);
3575 if (rc) {
3576 dprintk(VIDC_ERR, "Failed to get all regulators\n");
3577 return -ENODEV;
3578 }
3579
3580 rc = __init_clocks(device);
3581 if (rc) {
3582 dprintk(VIDC_ERR, "Failed to init clocks\n");
3583 rc = -ENODEV;
3584 goto err_init_clocks;
3585 }
3586
3587 rc = __init_bus(device);
3588 if (rc) {
3589 dprintk(VIDC_ERR, "Failed to init bus: %d\n", rc);
3590 goto err_init_bus;
3591 }
3592
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003593 rc = __init_subcaches(device);
3594 if (rc)
3595 dprintk(VIDC_WARN, "Failed to init subcaches: %d\n", rc);
3596
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003597 device->sys_init_capabilities =
3598 kzalloc(sizeof(struct msm_vidc_capability)
3599 * VIDC_MAX_SESSIONS, GFP_TEMPORARY);
3600
3601 return rc;
3602
3603err_init_bus:
3604 __deinit_clocks(device);
3605err_init_clocks:
3606 __deinit_regulators(device);
3607 return rc;
3608}
3609
3610static void __deinit_resources(struct venus_hfi_device *device)
3611{
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003612 __deinit_subcaches(device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003613 __deinit_bus(device);
3614 __deinit_clocks(device);
3615 __deinit_regulators(device);
3616 kfree(device->sys_init_capabilities);
3617 device->sys_init_capabilities = NULL;
3618}
3619
3620static int __protect_cp_mem(struct venus_hfi_device *device)
3621{
3622 struct tzbsp_memprot memprot;
3623 unsigned int resp = 0;
3624 int rc = 0;
3625 struct context_bank_info *cb;
3626 struct scm_desc desc = {0};
3627
3628 if (!device)
3629 return -EINVAL;
3630
3631 memprot.cp_start = 0x0;
3632 memprot.cp_size = 0x0;
3633 memprot.cp_nonpixel_start = 0x0;
3634 memprot.cp_nonpixel_size = 0x0;
3635
3636 list_for_each_entry(cb, &device->res->context_banks, list) {
3637 if (!strcmp(cb->name, "venus_ns")) {
3638 desc.args[1] = memprot.cp_size =
3639 cb->addr_range.start;
3640 dprintk(VIDC_DBG, "%s memprot.cp_size: %#x\n",
3641 __func__, memprot.cp_size);
3642 }
3643
3644 if (!strcmp(cb->name, "venus_sec_non_pixel")) {
3645 desc.args[2] = memprot.cp_nonpixel_start =
3646 cb->addr_range.start;
3647 desc.args[3] = memprot.cp_nonpixel_size =
3648 cb->addr_range.size;
3649 dprintk(VIDC_DBG,
3650 "%s memprot.cp_nonpixel_start: %#x size: %#x\n",
3651 __func__, memprot.cp_nonpixel_start,
3652 memprot.cp_nonpixel_size);
3653 }
3654 }
3655
3656 if (!is_scm_armv8()) {
3657 rc = scm_call(SCM_SVC_MP, TZBSP_MEM_PROTECT_VIDEO_VAR, &memprot,
3658 sizeof(memprot), &resp, sizeof(resp));
3659 } else {
3660 desc.arginfo = SCM_ARGS(4);
3661 rc = scm_call2(SCM_SIP_FNID(SCM_SVC_MP,
3662 TZBSP_MEM_PROTECT_VIDEO_VAR), &desc);
3663 resp = desc.ret[0];
3664 }
3665
3666 if (rc) {
3667 dprintk(VIDC_ERR, "Failed to protect memory(%d) response: %d\n",
3668 rc, resp);
3669 }
3670
3671 trace_venus_hfi_var_done(
3672 memprot.cp_start, memprot.cp_size,
3673 memprot.cp_nonpixel_start, memprot.cp_nonpixel_size);
3674 return rc;
3675}
3676
Surajit Podder29d7b342017-08-29 00:27:50 +05303677static int __disable_regulator(struct regulator_info *rinfo,
3678 struct venus_hfi_device *device)
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003679{
3680 int rc = 0;
3681
3682 dprintk(VIDC_DBG, "Disabling regulator %s\n", rinfo->name);
3683
3684 /*
3685 * This call is needed. Driver needs to acquire the control back
3686 * from HW in order to disable the regualtor. Else the behavior
3687 * is unknown.
3688 */
3689
Surajit Podder29d7b342017-08-29 00:27:50 +05303690 rc = __acquire_regulator(rinfo, device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003691 if (rc) {
3692 /*
3693 * This is somewhat fatal, but nothing we can do
3694 * about it. We can't disable the regulator w/o
3695 * getting it back under s/w control
3696 */
3697 dprintk(VIDC_WARN,
3698 "Failed to acquire control on %s\n",
3699 rinfo->name);
3700
3701 goto disable_regulator_failed;
3702 }
3703
3704 rc = regulator_disable(rinfo->regulator);
3705 if (rc) {
3706 dprintk(VIDC_WARN,
3707 "Failed to disable %s: %d\n",
3708 rinfo->name, rc);
3709 goto disable_regulator_failed;
3710 }
3711
3712 return 0;
3713disable_regulator_failed:
3714
3715 /* Bring attention to this issue */
Surajit Podder29d7b342017-08-29 00:27:50 +05303716 msm_vidc_res_handle_fatal_hw_error(device->res, true);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003717 return rc;
3718}
3719
3720static int __enable_hw_power_collapse(struct venus_hfi_device *device)
3721{
3722 int rc = 0;
3723
3724 if (!msm_vidc_fw_low_power_mode) {
3725 dprintk(VIDC_DBG, "Not enabling hardware power collapse\n");
3726 return 0;
3727 }
3728
3729 rc = __hand_off_regulators(device);
3730 if (rc)
3731 dprintk(VIDC_WARN,
3732 "%s : Failed to enable HW power collapse %d\n",
3733 __func__, rc);
3734 return rc;
3735}
3736
3737static int __enable_regulators(struct venus_hfi_device *device)
3738{
3739 int rc = 0, c = 0;
3740 struct regulator_info *rinfo;
3741
3742 dprintk(VIDC_DBG, "Enabling regulators\n");
3743
3744 venus_hfi_for_each_regulator(device, rinfo) {
3745 rc = regulator_enable(rinfo->regulator);
3746 if (rc) {
3747 dprintk(VIDC_ERR,
3748 "Failed to enable %s: %d\n",
3749 rinfo->name, rc);
3750 goto err_reg_enable_failed;
3751 }
3752
3753 dprintk(VIDC_DBG, "Enabled regulator %s\n",
3754 rinfo->name);
3755 c++;
3756 }
3757
3758 return 0;
3759
3760err_reg_enable_failed:
3761 venus_hfi_for_each_regulator_reverse_continue(device, rinfo, c)
Surajit Podder29d7b342017-08-29 00:27:50 +05303762 __disable_regulator(rinfo, device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003763
3764 return rc;
3765}
3766
3767static int __disable_regulators(struct venus_hfi_device *device)
3768{
3769 struct regulator_info *rinfo;
3770 int rc = 0;
3771
3772 dprintk(VIDC_DBG, "Disabling regulators\n");
3773
3774 venus_hfi_for_each_regulator_reverse(device, rinfo)
Surajit Podder29d7b342017-08-29 00:27:50 +05303775 __disable_regulator(rinfo, device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003776
3777 return rc;
3778}
3779
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003780static int __enable_subcaches(struct venus_hfi_device *device)
3781{
3782 int rc = 0;
3783 u32 c = 0;
3784 struct subcache_info *sinfo;
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003785
Shivendra Kakrania0634e2a2017-05-17 12:23:40 -07003786 if (msm_vidc_syscache_disable || !is_sys_cache_present(device))
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003787 return 0;
3788
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003789 /* Activate subcaches */
3790 venus_hfi_for_each_subcache(device, sinfo) {
3791 rc = llcc_slice_activate(sinfo->subcache);
3792 if (rc) {
3793 dprintk(VIDC_ERR, "Failed to activate %s: %d\n",
3794 sinfo->name, rc);
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07003795 goto err_activate_fail;
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003796 }
3797 sinfo->isactive = true;
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07003798 dprintk(VIDC_DBG, "Activated subcache %s\n", sinfo->name);
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003799 c++;
3800 }
3801
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07003802 dprintk(VIDC_DBG, "Activated %d Subcaches to Venus\n", c);
3803
3804 return 0;
3805
3806err_activate_fail:
3807 __release_subcaches(device);
3808 __disable_subcaches(device);
3809 return -EINVAL;
3810}
3811
3812static int __set_subcaches(struct venus_hfi_device *device)
3813{
3814 int rc = 0;
3815 u32 c = 0;
3816 struct subcache_info *sinfo;
3817 u32 resource[VIDC_MAX_SUBCACHE_SIZE];
3818 struct hfi_resource_syscache_info_type *sc_res_info;
3819 struct hfi_resource_subcache_type *sc_res;
3820 struct vidc_resource_hdr rhdr;
3821
3822 if (device->res->sys_cache_res_set) {
3823 dprintk(VIDC_DBG, "Subcaches already set to Venus\n");
3824 return 0;
3825 }
3826
3827 memset((void *)resource, 0x0, (sizeof(u32) * VIDC_MAX_SUBCACHE_SIZE));
3828
3829 sc_res_info = (struct hfi_resource_syscache_info_type *)resource;
3830 sc_res = &(sc_res_info->rg_subcache_entries[0]);
3831
3832 venus_hfi_for_each_subcache(device, sinfo) {
3833 if (sinfo->isactive == true) {
3834 sc_res[c].size = sinfo->subcache->llcc_slice_size;
3835 sc_res[c].sc_id = sinfo->subcache->llcc_slice_id;
3836 c++;
3837 }
3838 }
3839
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003840 /* Set resource to Venus for activated subcaches */
3841 if (c) {
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07003842 dprintk(VIDC_DBG, "Setting %d Subcaches\n", c);
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003843
3844 rhdr.resource_handle = sc_res_info; /* cookie */
3845 rhdr.resource_id = VIDC_RESOURCE_SYSCACHE;
3846
3847 sc_res_info->num_entries = c;
3848
3849 rc = __core_set_resource(device, &rhdr, (void *)sc_res_info);
3850 if (rc) {
3851 dprintk(VIDC_ERR, "Failed to set subcaches %d\n", rc);
3852 goto err_fail_set_subacaches;
3853 }
3854 }
3855
3856 venus_hfi_for_each_subcache(device, sinfo) {
3857 if (sinfo->isactive == true)
3858 sinfo->isset = true;
3859 }
3860
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07003861 dprintk(VIDC_DBG, "Set Subcaches done to Venus\n");
3862 device->res->sys_cache_res_set = true;
Shivendra Kakrania143d82b2017-05-18 10:37:56 -07003863
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003864 return 0;
3865
3866err_fail_set_subacaches:
3867 __disable_subcaches(device);
3868
3869 return rc;
3870}
3871
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07003872static int __release_subcaches(struct venus_hfi_device *device)
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003873{
3874 struct subcache_info *sinfo;
3875 int rc = 0;
3876 u32 c = 0;
3877 u32 resource[VIDC_MAX_SUBCACHE_SIZE];
3878 struct hfi_resource_syscache_info_type *sc_res_info;
3879 struct hfi_resource_subcache_type *sc_res;
3880 struct vidc_resource_hdr rhdr;
3881
Shivendra Kakrania0634e2a2017-05-17 12:23:40 -07003882 if (msm_vidc_syscache_disable || !is_sys_cache_present(device))
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003883 return 0;
3884
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003885 memset((void *)resource, 0x0, (sizeof(u32) * VIDC_MAX_SUBCACHE_SIZE));
3886
3887 sc_res_info = (struct hfi_resource_syscache_info_type *)resource;
3888 sc_res = &(sc_res_info->rg_subcache_entries[0]);
3889
3890 /* Release resource command to Venus */
3891 venus_hfi_for_each_subcache_reverse(device, sinfo) {
3892 if (sinfo->isset == true) {
3893 /* Update the entry */
3894 sc_res[c].size = sinfo->subcache->llcc_slice_size;
3895 sc_res[c].sc_id = sinfo->subcache->llcc_slice_id;
3896 c++;
3897 sinfo->isset = false;
3898 }
3899 }
3900
3901 if (c > 0) {
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07003902 dprintk(VIDC_DBG, "Releasing %d subcaches\n", c);
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003903 rhdr.resource_handle = sc_res_info; /* cookie */
3904 rhdr.resource_id = VIDC_RESOURCE_SYSCACHE;
3905
3906 rc = __core_release_resource(device, &rhdr);
3907 if (rc)
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07003908 dprintk(VIDC_ERR,
3909 "Failed to release %d subcaches\n", c);
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003910 }
3911
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07003912 device->res->sys_cache_res_set = false;
3913
3914 return rc;
3915}
3916
3917static int __disable_subcaches(struct venus_hfi_device *device)
3918{
3919 struct subcache_info *sinfo;
3920 int rc = 0;
3921
3922 if (msm_vidc_syscache_disable || !is_sys_cache_present(device))
3923 return 0;
3924
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07003925 /* De-activate subcaches */
3926 venus_hfi_for_each_subcache_reverse(device, sinfo) {
3927 if (sinfo->isactive == true) {
3928 dprintk(VIDC_DBG, "De-activate subcache %s\n",
3929 sinfo->name);
3930 rc = llcc_slice_deactivate(sinfo->subcache);
3931 if (rc) {
3932 dprintk(VIDC_ERR,
3933 "Failed to de-activate %s: %d\n",
3934 sinfo->name, rc);
3935 }
3936 sinfo->isactive = false;
3937 }
3938 }
3939
3940 return rc;
3941}
3942
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003943static int __venus_power_on(struct venus_hfi_device *device)
3944{
3945 int rc = 0;
3946
3947 if (device->power_enabled)
3948 return 0;
3949
3950 device->power_enabled = true;
3951 /* Vote for all hardware resources */
3952 rc = __vote_buses(device, device->bus_vote.data,
3953 device->bus_vote.data_count);
3954 if (rc) {
3955 dprintk(VIDC_ERR, "Failed to vote buses, err: %d\n", rc);
3956 goto fail_vote_buses;
3957 }
3958
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003959 rc = __enable_regulators(device);
3960 if (rc) {
3961 dprintk(VIDC_ERR, "Failed to enable GDSC, err = %d\n", rc);
3962 goto fail_enable_gdsc;
3963 }
3964
3965 rc = __prepare_enable_clks(device);
3966 if (rc) {
3967 dprintk(VIDC_ERR, "Failed to enable clocks: %d\n", rc);
3968 goto fail_enable_clks;
3969 }
3970
Praneeth Paladugub71968b2015-08-19 20:47:57 -07003971 rc = __scale_clocks(device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08003972 if (rc) {
3973 dprintk(VIDC_WARN,
3974 "Failed to scale clocks, performance might be affected\n");
3975 rc = 0;
3976 }
3977
3978 /*
3979 * Re-program all of the registers that get reset as a result of
3980 * regulator_disable() and _enable()
3981 */
3982 __set_registers(device);
3983
3984 __write_register(device, VIDC_WRAPPER_INTR_MASK,
3985 VIDC_WRAPPER_INTR_MASK_A2HVCODEC_BMSK);
3986 device->intr_status = 0;
3987 enable_irq(device->hal_data->irq);
3988
3989 /*
3990 * Hand off control of regulators to h/w _after_ enabling clocks.
3991 * Note that the GDSC will turn off when switching from normal
3992 * (s/w triggered) to fast (HW triggered) unless the h/w vote is
3993 * present. Since Venus isn't up yet, the GDSC will be off briefly.
3994 */
3995 if (__enable_hw_power_collapse(device))
3996 dprintk(VIDC_ERR, "Failed to enabled inter-frame PC\n");
3997
3998 return rc;
3999
4000fail_enable_clks:
4001 __disable_regulators(device);
4002fail_enable_gdsc:
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004003 __unvote_buses(device);
4004fail_vote_buses:
4005 device->power_enabled = false;
4006 return rc;
4007}
4008
Maheshwar Ajja1e0d5312017-04-14 12:14:30 -07004009static void __venus_power_off(struct venus_hfi_device *device)
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004010{
4011 if (!device->power_enabled)
4012 return;
4013
4014 if (!(device->intr_status & VIDC_WRAPPER_INTR_STATUS_A2HWD_BMSK))
4015 disable_irq_nosync(device->hal_data->irq);
4016 device->intr_status = 0;
4017
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004018 __disable_unprepare_clks(device);
4019 if (__disable_regulators(device))
4020 dprintk(VIDC_WARN, "Failed to disable regulators\n");
4021
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004022 if (__unvote_buses(device))
4023 dprintk(VIDC_WARN, "Failed to unvote for buses\n");
4024 device->power_enabled = false;
4025}
4026
4027static inline int __suspend(struct venus_hfi_device *device)
4028{
4029 int rc = 0;
4030
4031 if (!device) {
4032 dprintk(VIDC_ERR, "Invalid params: %pK\n", device);
4033 return -EINVAL;
4034 } else if (!device->power_enabled) {
4035 dprintk(VIDC_DBG, "Power already disabled\n");
4036 return 0;
4037 }
4038
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07004039 dprintk(VIDC_PROF, "Entering suspend\n");
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07004040
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004041 if (device->res->pm_qos_latency_us &&
4042 pm_qos_request_active(&device->qos))
4043 pm_qos_remove_request(&device->qos);
4044
4045 rc = __tzbsp_set_video_state(TZBSP_VIDEO_STATE_SUSPEND);
4046 if (rc) {
4047 dprintk(VIDC_WARN, "Failed to suspend video core %d\n", rc);
4048 goto err_tzbsp_suspend;
4049 }
4050
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07004051 __disable_subcaches(device);
4052
Maheshwar Ajja1e0d5312017-04-14 12:14:30 -07004053 __venus_power_off(device);
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07004054 dprintk(VIDC_PROF, "Venus power off\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004055 return rc;
4056
4057err_tzbsp_suspend:
4058 return rc;
4059}
4060
4061static inline int __resume(struct venus_hfi_device *device)
4062{
4063 int rc = 0;
4064
4065 if (!device) {
4066 dprintk(VIDC_ERR, "Invalid params: %pK\n", device);
4067 return -EINVAL;
4068 } else if (device->power_enabled) {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004069 goto exit;
4070 } else if (!__core_in_valid_state(device)) {
4071 dprintk(VIDC_DBG, "venus_hfi_device in deinit state.");
4072 return -EINVAL;
4073 }
4074
4075 dprintk(VIDC_PROF, "Resuming from power collapse\n");
4076 rc = __venus_power_on(device);
4077 if (rc) {
4078 dprintk(VIDC_ERR, "Failed to power on venus\n");
4079 goto err_venus_power_on;
4080 }
4081
4082 /* Reboot the firmware */
4083 rc = __tzbsp_set_video_state(TZBSP_VIDEO_STATE_RESUME);
4084 if (rc) {
4085 dprintk(VIDC_ERR, "Failed to resume video core %d\n", rc);
4086 goto err_set_video_state;
4087 }
4088
4089 __setup_ucregion_memory_map(device);
4090 /* Wait for boot completion */
4091 rc = __boot_firmware(device);
4092 if (rc) {
4093 dprintk(VIDC_ERR, "Failed to reset venus core\n");
4094 goto err_reset_core;
4095 }
4096
4097 /*
4098 * Work around for H/W bug, need to reprogram these registers once
4099 * firmware is out reset
4100 */
4101 __set_threshold_registers(device);
4102
4103 if (device->res->pm_qos_latency_us) {
4104#ifdef CONFIG_SMP
4105 device->qos.type = PM_QOS_REQ_AFFINE_IRQ;
4106 device->qos.irq = device->hal_data->irq;
4107#endif
4108 pm_qos_add_request(&device->qos, PM_QOS_CPU_DMA_LATENCY,
4109 device->res->pm_qos_latency_us);
4110 }
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07004111
4112 __sys_set_debug(device, msm_vidc_fw_debug);
4113
4114 rc = __enable_subcaches(device);
Shivendra Kakraniafd1dce32017-07-28 15:35:57 -07004115 if (!rc)
4116 __set_subcaches(device);
Shivendra Kakraniac1f60e02017-04-13 00:07:26 -07004117
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004118 dprintk(VIDC_PROF, "Resumed from power collapse\n");
4119exit:
Maheshwar Ajja1599fbf2017-08-24 13:28:17 -07004120 /* Don't reset skip_pc_count for SYS_PC_PREP cmd */
4121 if (device->last_packet_type != HFI_CMD_SYS_PC_PREP)
4122 device->skip_pc_count = 0;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004123 return rc;
4124err_reset_core:
4125 __tzbsp_set_video_state(TZBSP_VIDEO_STATE_SUSPEND);
4126err_set_video_state:
Maheshwar Ajja1e0d5312017-04-14 12:14:30 -07004127 __venus_power_off(device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004128err_venus_power_on:
4129 dprintk(VIDC_ERR, "Failed to resume from power collapse\n");
4130 return rc;
4131}
4132
4133static int __load_fw(struct venus_hfi_device *device)
4134{
4135 int rc = 0;
4136
4137 /* Initialize resources */
4138 rc = __init_resources(device, device->res);
4139 if (rc) {
4140 dprintk(VIDC_ERR, "Failed to init resources: %d\n", rc);
4141 goto fail_init_res;
4142 }
4143
4144 rc = __initialize_packetization(device);
4145 if (rc) {
4146 dprintk(VIDC_ERR, "Failed to initialize packetization\n");
4147 goto fail_init_pkt;
4148 }
4149 trace_msm_v4l2_vidc_fw_load_start("msm_v4l2_vidc venus_fw load start");
4150
4151 rc = __venus_power_on(device);
4152 if (rc) {
4153 dprintk(VIDC_ERR, "Failed to power on venus in in load_fw\n");
4154 goto fail_venus_power_on;
4155 }
4156
4157 if ((!device->res->use_non_secure_pil && !device->res->firmware_base)
4158 || device->res->use_non_secure_pil) {
4159 if (!device->resources.fw.cookie)
4160 device->resources.fw.cookie =
4161 subsystem_get_with_fwname("venus",
4162 device->res->fw_name);
4163
4164 if (IS_ERR_OR_NULL(device->resources.fw.cookie)) {
4165 dprintk(VIDC_ERR, "Failed to download firmware\n");
4166 device->resources.fw.cookie = NULL;
4167 rc = -ENOMEM;
4168 goto fail_load_fw;
4169 }
4170 }
4171
4172 if (!device->res->use_non_secure_pil && !device->res->firmware_base) {
4173 rc = __protect_cp_mem(device);
4174 if (rc) {
4175 dprintk(VIDC_ERR, "Failed to protect memory\n");
4176 goto fail_protect_mem;
4177 }
4178 }
4179 trace_msm_v4l2_vidc_fw_load_end("msm_v4l2_vidc venus_fw load end");
4180 return rc;
4181fail_protect_mem:
4182 if (device->resources.fw.cookie)
4183 subsystem_put(device->resources.fw.cookie);
4184 device->resources.fw.cookie = NULL;
4185fail_load_fw:
Maheshwar Ajja1e0d5312017-04-14 12:14:30 -07004186 __venus_power_off(device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004187fail_venus_power_on:
4188fail_init_pkt:
4189 __deinit_resources(device);
4190fail_init_res:
4191 trace_msm_v4l2_vidc_fw_load_end("msm_v4l2_vidc venus_fw load end");
4192 return rc;
4193}
4194
4195static void __unload_fw(struct venus_hfi_device *device)
4196{
4197 if (!device->resources.fw.cookie)
4198 return;
4199
4200 cancel_delayed_work(&venus_hfi_pm_work);
4201 if (device->state != VENUS_STATE_DEINIT)
4202 flush_workqueue(device->venus_pm_workq);
4203
4204 __vote_buses(device, NULL, 0);
4205 subsystem_put(device->resources.fw.cookie);
4206 __interface_queues_release(device);
Maheshwar Ajja1e0d5312017-04-14 12:14:30 -07004207 __venus_power_off(device);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004208 device->resources.fw.cookie = NULL;
4209 __deinit_resources(device);
Maheshwar Ajjae43dd822017-04-25 15:56:53 -07004210
4211 dprintk(VIDC_PROF, "Firmware unloaded successfully\n");
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004212}
4213
4214static int venus_hfi_get_fw_info(void *dev, struct hal_fw_info *fw_info)
4215{
4216 int i = 0, j = 0;
4217 struct venus_hfi_device *device = dev;
4218 u32 smem_block_size = 0;
4219 u8 *smem_table_ptr;
Deepak Kushwah9d4c7fa2017-03-07 10:48:45 +05304220 char version[VENUS_VERSION_LENGTH] = "";
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004221 const u32 smem_image_index_venus = 14 * 128;
4222
4223 if (!device || !fw_info) {
4224 dprintk(VIDC_ERR,
4225 "%s Invalid parameter: device = %pK fw_info = %pK\n",
4226 __func__, device, fw_info);
4227 return -EINVAL;
4228 }
4229
4230 mutex_lock(&device->lock);
4231
4232 smem_table_ptr = smem_get_entry(SMEM_IMAGE_VERSION_TABLE,
4233 &smem_block_size, 0, SMEM_ANY_HOST_FLAG);
4234 if (smem_table_ptr &&
4235 ((smem_image_index_venus +
4236 VENUS_VERSION_LENGTH) <= smem_block_size))
4237 memcpy(version,
4238 smem_table_ptr + smem_image_index_venus,
4239 VENUS_VERSION_LENGTH);
4240
4241 while (version[i++] != 'V' && i < VENUS_VERSION_LENGTH)
4242 ;
4243
4244 if (i == VENUS_VERSION_LENGTH - 1) {
4245 dprintk(VIDC_WARN, "Venus version string is not proper\n");
4246 fw_info->version[0] = '\0';
4247 goto fail_version_string;
4248 }
4249
4250 for (i--; i < VENUS_VERSION_LENGTH && j < VENUS_VERSION_LENGTH - 1; i++)
4251 fw_info->version[j++] = version[i];
4252 fw_info->version[j] = '\0';
4253
4254fail_version_string:
4255 dprintk(VIDC_DBG, "F/W version retrieved : %s\n", fw_info->version);
4256 fw_info->base_addr = device->hal_data->firmware_base;
4257 fw_info->register_base = device->res->register_base;
4258 fw_info->register_size = device->hal_data->register_size;
4259 fw_info->irq = device->hal_data->irq;
4260
4261 mutex_unlock(&device->lock);
4262 return 0;
4263}
4264
4265static int venus_hfi_get_core_capabilities(void *dev)
4266{
4267 struct venus_hfi_device *device = dev;
4268 int rc = 0;
4269
4270 if (!device)
4271 return -EINVAL;
4272
4273 mutex_lock(&device->lock);
4274
4275 rc = HAL_VIDEO_ENCODER_ROTATION_CAPABILITY |
4276 HAL_VIDEO_ENCODER_SCALING_CAPABILITY |
4277 HAL_VIDEO_ENCODER_DEINTERLACE_CAPABILITY |
4278 HAL_VIDEO_DECODER_MULTI_STREAM_CAPABILITY;
4279
4280 mutex_unlock(&device->lock);
4281
4282 return rc;
4283}
4284
Maheshwar Ajja9ff81a22017-08-05 13:25:55 -07004285static int venus_hfi_noc_error_info(void *dev)
4286{
4287 struct venus_hfi_device *device;
4288 u32 val = 0;
4289
4290 if (!dev) {
4291 dprintk(VIDC_ERR, "%s: null device\n", __func__);
4292 return -EINVAL;
4293 }
4294 device = dev;
4295
4296 mutex_lock(&device->lock);
4297 dprintk(VIDC_ERR, "%s: non error information\n", __func__);
4298
4299 val = __read_register(device, 0x0C500);
4300 dprintk(VIDC_ERR, "NOC_ERR_SWID_LOW(0x00AA0C500): %#x\n", val);
4301
4302 val = __read_register(device, 0x0C504);
4303 dprintk(VIDC_ERR, "NOC_ERR_SWID_HIGH(0x00AA0C504): %#x\n", val);
4304
4305 val = __read_register(device, 0x0C508);
4306 dprintk(VIDC_ERR, "NOC_ERR_MAINCTL_LOW(0x00AA0C508): %#x\n", val);
4307
4308 val = __read_register(device, 0x0C510);
4309 dprintk(VIDC_ERR, "NOC_ERR_ERRVLD_LOW(0x00AA0C510): %#x\n", val);
4310
4311 val = __read_register(device, 0x0C518);
4312 dprintk(VIDC_ERR, "NOC_ERR_ERRCLR_LOW(0x00AA0C518): %#x\n", val);
4313
4314 val = __read_register(device, 0x0C520);
4315 dprintk(VIDC_ERR, "NOC_ERR_ERRLOG0_LOW(0x00AA0C520): %#x\n", val);
4316
4317 val = __read_register(device, 0x0C524);
4318 dprintk(VIDC_ERR, "NOC_ERR_ERRLOG0_HIGH(0x00AA0C524): %#x\n", val);
4319
4320 val = __read_register(device, 0x0C528);
4321 dprintk(VIDC_ERR, "NOC_ERR_ERRLOG1_LOW(0x00AA0C528): %#x\n", val);
4322
4323 val = __read_register(device, 0x0C52C);
4324 dprintk(VIDC_ERR, "NOC_ERR_ERRLOG1_HIGH(0x00AA0C52C): %#x\n", val);
4325
4326 val = __read_register(device, 0x0C530);
4327 dprintk(VIDC_ERR, "NOC_ERR_ERRLOG2_LOW(0x00AA0C530): %#x\n", val);
4328
4329 val = __read_register(device, 0x0C534);
4330 dprintk(VIDC_ERR, "NOC_ERR_ERRLOG2_HIGH(0x00AA0C534): %#x\n", val);
4331
4332 val = __read_register(device, 0x0C538);
4333 dprintk(VIDC_ERR, "NOC_ERR_ERRLOG3_LOW(0x00AA0C538): %#x\n", val);
4334
4335 val = __read_register(device, 0x0C53C);
4336 dprintk(VIDC_ERR, "NOC_ERR_ERRLOG3_HIGH(0x00AA0C53C): %#x\n", val);
4337
4338 mutex_unlock(&device->lock);
4339
4340 return 0;
4341}
4342
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004343static int __initialize_packetization(struct venus_hfi_device *device)
4344{
4345 int rc = 0;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004346
4347 if (!device || !device->res) {
4348 dprintk(VIDC_ERR, "%s - invalid param\n", __func__);
4349 return -EINVAL;
4350 }
4351
Chinmay Sawarkar2de3f772017-02-07 12:03:44 -08004352 device->packetization_type = HFI_PACKETIZATION_4XX;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004353
4354 device->pkt_ops = hfi_get_pkt_ops_handle(device->packetization_type);
4355 if (!device->pkt_ops) {
4356 rc = -EINVAL;
4357 dprintk(VIDC_ERR, "Failed to get pkt_ops handle\n");
4358 }
4359
4360 return rc;
4361}
4362
4363static struct venus_hfi_device *__add_device(u32 device_id,
4364 struct msm_vidc_platform_resources *res,
4365 hfi_cmd_response_callback callback)
4366{
4367 struct venus_hfi_device *hdevice = NULL;
4368 int rc = 0;
4369
4370 if (!res || !callback) {
4371 dprintk(VIDC_ERR, "Invalid Parameters\n");
4372 return NULL;
4373 }
4374
4375 dprintk(VIDC_INFO, "entered , device_id: %d\n", device_id);
4376
4377 hdevice = (struct venus_hfi_device *)
4378 kzalloc(sizeof(struct venus_hfi_device), GFP_KERNEL);
4379 if (!hdevice) {
4380 dprintk(VIDC_ERR, "failed to allocate new device\n");
4381 goto exit;
4382 }
4383
4384 hdevice->response_pkt = kmalloc_array(max_packets,
4385 sizeof(*hdevice->response_pkt), GFP_KERNEL);
4386 if (!hdevice->response_pkt) {
4387 dprintk(VIDC_ERR, "failed to allocate response_pkt\n");
4388 goto err_cleanup;
4389 }
4390
4391 hdevice->raw_packet =
4392 kzalloc(VIDC_IFACEQ_VAR_HUGE_PKT_SIZE, GFP_TEMPORARY);
4393 if (!hdevice->raw_packet) {
4394 dprintk(VIDC_ERR, "failed to allocate raw packet\n");
4395 goto err_cleanup;
4396 }
4397
4398 rc = __init_regs_and_interrupts(hdevice, res);
4399 if (rc)
4400 goto err_cleanup;
4401
4402 hdevice->res = res;
4403 hdevice->device_id = device_id;
4404 hdevice->callback = callback;
4405
4406 hdevice->vidc_workq = create_singlethread_workqueue(
4407 "msm_vidc_workerq_venus");
4408 if (!hdevice->vidc_workq) {
4409 dprintk(VIDC_ERR, ": create vidc workq failed\n");
4410 goto err_cleanup;
4411 }
4412
4413 hdevice->venus_pm_workq = create_singlethread_workqueue(
4414 "pm_workerq_venus");
4415 if (!hdevice->venus_pm_workq) {
4416 dprintk(VIDC_ERR, ": create pm workq failed\n");
4417 goto err_cleanup;
4418 }
4419
4420 if (!hal_ctxt.dev_count)
4421 INIT_LIST_HEAD(&hal_ctxt.dev_head);
4422
4423 mutex_init(&hdevice->lock);
4424 INIT_LIST_HEAD(&hdevice->list);
4425 INIT_LIST_HEAD(&hdevice->sess_head);
4426 list_add_tail(&hdevice->list, &hal_ctxt.dev_head);
4427 hal_ctxt.dev_count++;
4428
4429 return hdevice;
4430
4431err_cleanup:
4432 if (hdevice->vidc_workq)
4433 destroy_workqueue(hdevice->vidc_workq);
4434 kfree(hdevice->response_pkt);
4435 kfree(hdevice->raw_packet);
4436 kfree(hdevice);
4437exit:
4438 return NULL;
4439}
4440
4441static struct venus_hfi_device *__get_device(u32 device_id,
4442 struct msm_vidc_platform_resources *res,
4443 hfi_cmd_response_callback callback)
4444{
4445 if (!res || !callback) {
4446 dprintk(VIDC_ERR, "Invalid params: %pK %pK\n", res, callback);
4447 return NULL;
4448 }
4449
4450 return __add_device(device_id, res, callback);
4451}
4452
4453void venus_hfi_delete_device(void *device)
4454{
4455 struct venus_hfi_device *close, *tmp, *dev;
4456
4457 if (!device)
4458 return;
4459
4460 dev = (struct venus_hfi_device *) device;
4461
4462 mutex_lock(&dev->lock);
4463 __iommu_detach(dev);
4464 mutex_unlock(&dev->lock);
4465
4466 list_for_each_entry_safe(close, tmp, &hal_ctxt.dev_head, list) {
4467 if (close->hal_data->irq == dev->hal_data->irq) {
4468 hal_ctxt.dev_count--;
4469 list_del(&close->list);
4470 mutex_destroy(&close->lock);
4471 destroy_workqueue(close->vidc_workq);
4472 destroy_workqueue(close->venus_pm_workq);
4473 free_irq(dev->hal_data->irq, close);
4474 iounmap(dev->hal_data->register_base);
4475 kfree(close->hal_data);
4476 kfree(close->response_pkt);
4477 kfree(close->raw_packet);
4478 kfree(close);
4479 break;
4480 }
4481 }
4482}
4483
4484static void venus_init_hfi_callbacks(struct hfi_device *hdev)
4485{
4486 hdev->core_init = venus_hfi_core_init;
4487 hdev->core_release = venus_hfi_core_release;
4488 hdev->core_ping = venus_hfi_core_ping;
4489 hdev->core_trigger_ssr = venus_hfi_core_trigger_ssr;
4490 hdev->session_init = venus_hfi_session_init;
4491 hdev->session_end = venus_hfi_session_end;
4492 hdev->session_abort = venus_hfi_session_abort;
4493 hdev->session_clean = venus_hfi_session_clean;
4494 hdev->session_set_buffers = venus_hfi_session_set_buffers;
4495 hdev->session_release_buffers = venus_hfi_session_release_buffers;
4496 hdev->session_load_res = venus_hfi_session_load_res;
4497 hdev->session_release_res = venus_hfi_session_release_res;
4498 hdev->session_start = venus_hfi_session_start;
4499 hdev->session_continue = venus_hfi_session_continue;
4500 hdev->session_stop = venus_hfi_session_stop;
4501 hdev->session_etb = venus_hfi_session_etb;
4502 hdev->session_ftb = venus_hfi_session_ftb;
4503 hdev->session_process_batch = venus_hfi_session_process_batch;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004504 hdev->session_get_buf_req = venus_hfi_session_get_buf_req;
4505 hdev->session_flush = venus_hfi_session_flush;
4506 hdev->session_set_property = venus_hfi_session_set_property;
4507 hdev->session_get_property = venus_hfi_session_get_property;
4508 hdev->scale_clocks = venus_hfi_scale_clocks;
4509 hdev->vote_bus = venus_hfi_vote_buses;
4510 hdev->get_fw_info = venus_hfi_get_fw_info;
4511 hdev->get_core_capabilities = venus_hfi_get_core_capabilities;
4512 hdev->suspend = venus_hfi_suspend;
4513 hdev->flush_debug_queue = venus_hfi_flush_debug_queue;
Maheshwar Ajja9ff81a22017-08-05 13:25:55 -07004514 hdev->noc_error_info = venus_hfi_noc_error_info;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08004515 hdev->get_default_properties = venus_hfi_get_default_properties;
4516}
4517
4518int venus_hfi_initialize(struct hfi_device *hdev, u32 device_id,
4519 struct msm_vidc_platform_resources *res,
4520 hfi_cmd_response_callback callback)
4521{
4522 int rc = 0;
4523
4524 if (!hdev || !res || !callback) {
4525 dprintk(VIDC_ERR, "Invalid params: %pK %pK %pK\n",
4526 hdev, res, callback);
4527 rc = -EINVAL;
4528 goto err_venus_hfi_init;
4529 }
4530
4531 hdev->hfi_device_data = __get_device(device_id, res, callback);
4532
4533 if (IS_ERR_OR_NULL(hdev->hfi_device_data)) {
4534 rc = PTR_ERR(hdev->hfi_device_data) ?: -EINVAL;
4535 goto err_venus_hfi_init;
4536 }
4537
4538 venus_init_hfi_callbacks(hdev);
4539
4540err_venus_hfi_init:
4541 return rc;
4542}
4543