Merge remote-tracking branch 'lsk/v3.10/topic/arm64-topology' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / radeon / radeon_uvd.c
1 /*
2  * Copyright 2011 Advanced Micro Devices, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * The above copyright notice and this permission notice (including the
22  * next paragraph) shall be included in all copies or substantial portions
23  * of the Software.
24  *
25  */
26 /*
27  * Authors:
28  *    Christian König <deathsimple@vodafone.de>
29  */
30
31 #include <linux/firmware.h>
32 #include <linux/module.h>
33 #include <drm/drmP.h>
34 #include <drm/drm.h>
35
36 #include "radeon.h"
37 #include "r600d.h"
38
39 /* 1 second timeout */
40 #define UVD_IDLE_TIMEOUT_MS     1000
41
42 /* Firmware Names */
43 #define FIRMWARE_RV710          "radeon/RV710_uvd.bin"
44 #define FIRMWARE_CYPRESS        "radeon/CYPRESS_uvd.bin"
45 #define FIRMWARE_SUMO           "radeon/SUMO_uvd.bin"
46 #define FIRMWARE_TAHITI         "radeon/TAHITI_uvd.bin"
47
48 MODULE_FIRMWARE(FIRMWARE_RV710);
49 MODULE_FIRMWARE(FIRMWARE_CYPRESS);
50 MODULE_FIRMWARE(FIRMWARE_SUMO);
51 MODULE_FIRMWARE(FIRMWARE_TAHITI);
52
53 static void radeon_uvd_idle_work_handler(struct work_struct *work);
54
55 int radeon_uvd_init(struct radeon_device *rdev)
56 {
57         struct platform_device *pdev;
58         unsigned long bo_size;
59         const char *fw_name;
60         int i, r;
61
62         INIT_DELAYED_WORK(&rdev->uvd.idle_work, radeon_uvd_idle_work_handler);
63
64         pdev = platform_device_register_simple("radeon_uvd", 0, NULL, 0);
65         r = IS_ERR(pdev);
66         if (r) {
67                 dev_err(rdev->dev, "radeon_uvd: Failed to register firmware\n");
68                 return -EINVAL;
69         }
70
71         switch (rdev->family) {
72         case CHIP_RV710:
73         case CHIP_RV730:
74         case CHIP_RV740:
75                 fw_name = FIRMWARE_RV710;
76                 break;
77
78         case CHIP_CYPRESS:
79         case CHIP_HEMLOCK:
80         case CHIP_JUNIPER:
81         case CHIP_REDWOOD:
82         case CHIP_CEDAR:
83                 fw_name = FIRMWARE_CYPRESS;
84                 break;
85
86         case CHIP_SUMO:
87         case CHIP_SUMO2:
88         case CHIP_PALM:
89         case CHIP_CAYMAN:
90         case CHIP_BARTS:
91         case CHIP_TURKS:
92         case CHIP_CAICOS:
93                 fw_name = FIRMWARE_SUMO;
94                 break;
95
96         case CHIP_TAHITI:
97         case CHIP_VERDE:
98         case CHIP_PITCAIRN:
99         case CHIP_ARUBA:
100                 fw_name = FIRMWARE_TAHITI;
101                 break;
102
103         default:
104                 return -EINVAL;
105         }
106
107         r = request_firmware(&rdev->uvd_fw, fw_name, &pdev->dev);
108         if (r) {
109                 dev_err(rdev->dev, "radeon_uvd: Can't load firmware \"%s\"\n",
110                         fw_name);
111                 platform_device_unregister(pdev);
112                 return r;
113         }
114
115         platform_device_unregister(pdev);
116
117         bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 8) +
118                   RADEON_UVD_STACK_SIZE + RADEON_UVD_HEAP_SIZE;
119         r = radeon_bo_create(rdev, bo_size, PAGE_SIZE, true,
120                              RADEON_GEM_DOMAIN_VRAM, NULL, &rdev->uvd.vcpu_bo);
121         if (r) {
122                 dev_err(rdev->dev, "(%d) failed to allocate UVD bo\n", r);
123                 return r;
124         }
125
126         r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
127         if (r) {
128                 radeon_bo_unref(&rdev->uvd.vcpu_bo);
129                 dev_err(rdev->dev, "(%d) failed to reserve UVD bo\n", r);
130                 return r;
131         }
132
133         r = radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
134                           &rdev->uvd.gpu_addr);
135         if (r) {
136                 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
137                 radeon_bo_unref(&rdev->uvd.vcpu_bo);
138                 dev_err(rdev->dev, "(%d) UVD bo pin failed\n", r);
139                 return r;
140         }
141
142         r = radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
143         if (r) {
144                 dev_err(rdev->dev, "(%d) UVD map failed\n", r);
145                 return r;
146         }
147
148         radeon_bo_unreserve(rdev->uvd.vcpu_bo);
149
150         for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
151                 atomic_set(&rdev->uvd.handles[i], 0);
152                 rdev->uvd.filp[i] = NULL;
153         }
154
155         return 0;
156 }
157
158 void radeon_uvd_fini(struct radeon_device *rdev)
159 {
160         int r;
161
162         if (rdev->uvd.vcpu_bo == NULL)
163                 return;
164
165         r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
166         if (!r) {
167                 radeon_bo_kunmap(rdev->uvd.vcpu_bo);
168                 radeon_bo_unpin(rdev->uvd.vcpu_bo);
169                 radeon_bo_unreserve(rdev->uvd.vcpu_bo);
170         }
171
172         radeon_bo_unref(&rdev->uvd.vcpu_bo);
173
174         release_firmware(rdev->uvd_fw);
175 }
176
177 int radeon_uvd_suspend(struct radeon_device *rdev)
178 {
179         unsigned size;
180         void *ptr;
181         int i;
182
183         if (rdev->uvd.vcpu_bo == NULL)
184                 return 0;
185
186         for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
187                 if (atomic_read(&rdev->uvd.handles[i]))
188                         break;
189
190         if (i == RADEON_MAX_UVD_HANDLES)
191                 return 0;
192
193         size = radeon_bo_size(rdev->uvd.vcpu_bo);
194         size -= rdev->uvd_fw->size;
195
196         ptr = rdev->uvd.cpu_addr;
197         ptr += rdev->uvd_fw->size;
198
199         rdev->uvd.saved_bo = kmalloc(size, GFP_KERNEL);
200         memcpy(rdev->uvd.saved_bo, ptr, size);
201
202         return 0;
203 }
204
205 int radeon_uvd_resume(struct radeon_device *rdev)
206 {
207         unsigned size;
208         void *ptr;
209
210         if (rdev->uvd.vcpu_bo == NULL)
211                 return -EINVAL;
212
213         memcpy(rdev->uvd.cpu_addr, rdev->uvd_fw->data, rdev->uvd_fw->size);
214
215         size = radeon_bo_size(rdev->uvd.vcpu_bo);
216         size -= rdev->uvd_fw->size;
217
218         ptr = rdev->uvd.cpu_addr;
219         ptr += rdev->uvd_fw->size;
220
221         if (rdev->uvd.saved_bo != NULL) {
222                 memcpy(ptr, rdev->uvd.saved_bo, size);
223                 kfree(rdev->uvd.saved_bo);
224                 rdev->uvd.saved_bo = NULL;
225         } else
226                 memset(ptr, 0, size);
227
228         return 0;
229 }
230
231 void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo)
232 {
233         rbo->placement.fpfn = 0 >> PAGE_SHIFT;
234         rbo->placement.lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
235 }
236
237 void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
238 {
239         int i, r;
240         for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
241                 uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
242                 if (handle != 0 && rdev->uvd.filp[i] == filp) {
243                         struct radeon_fence *fence;
244
245                         radeon_uvd_note_usage(rdev);
246
247                         r = radeon_uvd_get_destroy_msg(rdev,
248                                 R600_RING_TYPE_UVD_INDEX, handle, &fence);
249                         if (r) {
250                                 DRM_ERROR("Error destroying UVD (%d)!\n", r);
251                                 continue;
252                         }
253
254                         radeon_fence_wait(fence, false);
255                         radeon_fence_unref(&fence);
256
257                         rdev->uvd.filp[i] = NULL;
258                         atomic_set(&rdev->uvd.handles[i], 0);
259                 }
260         }
261 }
262
263 static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
264 {
265         unsigned stream_type = msg[4];
266         unsigned width = msg[6];
267         unsigned height = msg[7];
268         unsigned dpb_size = msg[9];
269         unsigned pitch = msg[28];
270
271         unsigned width_in_mb = width / 16;
272         unsigned height_in_mb = ALIGN(height / 16, 2);
273
274         unsigned image_size, tmp, min_dpb_size;
275
276         image_size = width * height;
277         image_size += image_size / 2;
278         image_size = ALIGN(image_size, 1024);
279
280         switch (stream_type) {
281         case 0: /* H264 */
282
283                 /* reference picture buffer */
284                 min_dpb_size = image_size * 17;
285
286                 /* macroblock context buffer */
287                 min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
288
289                 /* IT surface buffer */
290                 min_dpb_size += width_in_mb * height_in_mb * 32;
291                 break;
292
293         case 1: /* VC1 */
294
295                 /* reference picture buffer */
296                 min_dpb_size = image_size * 3;
297
298                 /* CONTEXT_BUFFER */
299                 min_dpb_size += width_in_mb * height_in_mb * 128;
300
301                 /* IT surface buffer */
302                 min_dpb_size += width_in_mb * 64;
303
304                 /* DB surface buffer */
305                 min_dpb_size += width_in_mb * 128;
306
307                 /* BP */
308                 tmp = max(width_in_mb, height_in_mb);
309                 min_dpb_size += ALIGN(tmp * 7 * 16, 64);
310                 break;
311
312         case 3: /* MPEG2 */
313
314                 /* reference picture buffer */
315                 min_dpb_size = image_size * 3;
316                 break;
317
318         case 4: /* MPEG4 */
319
320                 /* reference picture buffer */
321                 min_dpb_size = image_size * 3;
322
323                 /* CM */
324                 min_dpb_size += width_in_mb * height_in_mb * 64;
325
326                 /* IT surface buffer */
327                 min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
328                 break;
329
330         default:
331                 DRM_ERROR("UVD codec not handled %d!\n", stream_type);
332                 return -EINVAL;
333         }
334
335         if (width > pitch) {
336                 DRM_ERROR("Invalid UVD decoding target pitch!\n");
337                 return -EINVAL;
338         }
339
340         if (dpb_size < min_dpb_size) {
341                 DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
342                           dpb_size, min_dpb_size);
343                 return -EINVAL;
344         }
345
346         buf_sizes[0x1] = dpb_size;
347         buf_sizes[0x2] = image_size;
348         return 0;
349 }
350
351 static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
352                              unsigned offset, unsigned buf_sizes[])
353 {
354         int32_t *msg, msg_type, handle;
355         void *ptr;
356
357         int i, r;
358
359         if (offset & 0x3F) {
360                 DRM_ERROR("UVD messages must be 64 byte aligned!\n");
361                 return -EINVAL;
362         }
363
364         if (bo->tbo.sync_obj) {
365                 r = radeon_fence_wait(bo->tbo.sync_obj, false);
366                 if (r) {
367                         DRM_ERROR("Failed waiting for UVD message (%d)!\n", r);
368                         return r;
369                 }
370         }
371
372         r = radeon_bo_kmap(bo, &ptr);
373         if (r)
374                 return r;
375
376         msg = ptr + offset;
377
378         msg_type = msg[1];
379         handle = msg[2];
380
381         if (handle == 0) {
382                 DRM_ERROR("Invalid UVD handle!\n");
383                 return -EINVAL;
384         }
385
386         if (msg_type == 1) {
387                 /* it's a decode msg, calc buffer sizes */
388                 r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
389                 radeon_bo_kunmap(bo);
390                 if (r)
391                         return r;
392
393         } else if (msg_type == 2) {
394                 /* it's a destroy msg, free the handle */
395                 for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
396                         atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
397                 radeon_bo_kunmap(bo);
398                 return 0;
399         } else {
400                 /* it's a create msg, no special handling needed */
401                 radeon_bo_kunmap(bo);
402         }
403
404         /* create or decode, validate the handle */
405         for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
406                 if (atomic_read(&p->rdev->uvd.handles[i]) == handle)
407                         return 0;
408         }
409
410         /* handle not found try to alloc a new one */
411         for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
412                 if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
413                         p->rdev->uvd.filp[i] = p->filp;
414                         return 0;
415                 }
416         }
417
418         DRM_ERROR("No more free UVD handles!\n");
419         return -EINVAL;
420 }
421
422 static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
423                                int data0, int data1,
424                                unsigned buf_sizes[])
425 {
426         struct radeon_cs_chunk *relocs_chunk;
427         struct radeon_cs_reloc *reloc;
428         unsigned idx, cmd, offset;
429         uint64_t start, end;
430         int r;
431
432         relocs_chunk = &p->chunks[p->chunk_relocs_idx];
433         offset = radeon_get_ib_value(p, data0);
434         idx = radeon_get_ib_value(p, data1);
435         if (idx >= relocs_chunk->length_dw) {
436                 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
437                           idx, relocs_chunk->length_dw);
438                 return -EINVAL;
439         }
440
441         reloc = p->relocs_ptr[(idx / 4)];
442         start = reloc->lobj.gpu_offset;
443         end = start + radeon_bo_size(reloc->robj);
444         start += offset;
445
446         p->ib.ptr[data0] = start & 0xFFFFFFFF;
447         p->ib.ptr[data1] = start >> 32;
448
449         cmd = radeon_get_ib_value(p, p->idx) >> 1;
450
451         if (cmd < 0x4) {
452                 if ((end - start) < buf_sizes[cmd]) {
453                         DRM_ERROR("buffer to small (%d / %d)!\n",
454                                   (unsigned)(end - start), buf_sizes[cmd]);
455                         return -EINVAL;
456                 }
457
458         } else if (cmd != 0x100) {
459                 DRM_ERROR("invalid UVD command %X!\n", cmd);
460                 return -EINVAL;
461         }
462
463         if ((start >> 28) != (end >> 28)) {
464                 DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
465                           start, end);
466                 return -EINVAL;
467         }
468
469         /* TODO: is this still necessary on NI+ ? */
470         if ((cmd == 0 || cmd == 0x3) &&
471             (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
472                 DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
473                           start, end);
474                 return -EINVAL;
475         }
476
477         if (cmd == 0) {
478                 r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
479                 if (r)
480                         return r;
481         }
482
483         return 0;
484 }
485
486 static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
487                              struct radeon_cs_packet *pkt,
488                              int *data0, int *data1,
489                              unsigned buf_sizes[])
490 {
491         int i, r;
492
493         p->idx++;
494         for (i = 0; i <= pkt->count; ++i) {
495                 switch (pkt->reg + i*4) {
496                 case UVD_GPCOM_VCPU_DATA0:
497                         *data0 = p->idx;
498                         break;
499                 case UVD_GPCOM_VCPU_DATA1:
500                         *data1 = p->idx;
501                         break;
502                 case UVD_GPCOM_VCPU_CMD:
503                         r = radeon_uvd_cs_reloc(p, *data0, *data1, buf_sizes);
504                         if (r)
505                                 return r;
506                         break;
507                 case UVD_ENGINE_CNTL:
508                         break;
509                 default:
510                         DRM_ERROR("Invalid reg 0x%X!\n",
511                                   pkt->reg + i*4);
512                         return -EINVAL;
513                 }
514                 p->idx++;
515         }
516         return 0;
517 }
518
519 int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
520 {
521         struct radeon_cs_packet pkt;
522         int r, data0 = 0, data1 = 0;
523
524         /* minimum buffer sizes */
525         unsigned buf_sizes[] = {
526                 [0x00000000]    =       2048,
527                 [0x00000001]    =       32 * 1024 * 1024,
528                 [0x00000002]    =       2048 * 1152 * 3,
529                 [0x00000003]    =       2048,
530         };
531
532         if (p->chunks[p->chunk_ib_idx].length_dw % 16) {
533                 DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
534                           p->chunks[p->chunk_ib_idx].length_dw);
535                 return -EINVAL;
536         }
537
538         if (p->chunk_relocs_idx == -1) {
539                 DRM_ERROR("No relocation chunk !\n");
540                 return -EINVAL;
541         }
542
543
544         do {
545                 r = radeon_cs_packet_parse(p, &pkt, p->idx);
546                 if (r)
547                         return r;
548                 switch (pkt.type) {
549                 case RADEON_PACKET_TYPE0:
550                         r = radeon_uvd_cs_reg(p, &pkt, &data0,
551                                               &data1, buf_sizes);
552                         if (r)
553                                 return r;
554                         break;
555                 case RADEON_PACKET_TYPE2:
556                         p->idx += pkt.count + 2;
557                         break;
558                 default:
559                         DRM_ERROR("Unknown packet type %d !\n", pkt.type);
560                         return -EINVAL;
561                 }
562         } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
563         return 0;
564 }
565
566 static int radeon_uvd_send_msg(struct radeon_device *rdev,
567                                int ring, struct radeon_bo *bo,
568                                struct radeon_fence **fence)
569 {
570         struct ttm_validate_buffer tv;
571         struct list_head head;
572         struct radeon_ib ib;
573         uint64_t addr;
574         int i, r;
575
576         memset(&tv, 0, sizeof(tv));
577         tv.bo = &bo->tbo;
578
579         INIT_LIST_HEAD(&head);
580         list_add(&tv.head, &head);
581
582         r = ttm_eu_reserve_buffers(&head);
583         if (r)
584                 return r;
585
586         radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_VRAM);
587         radeon_uvd_force_into_uvd_segment(bo);
588
589         r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
590         if (r) {
591                 ttm_eu_backoff_reservation(&head);
592                 return r;
593         }
594
595         r = radeon_ib_get(rdev, ring, &ib, NULL, 16);
596         if (r) {
597                 ttm_eu_backoff_reservation(&head);
598                 return r;
599         }
600
601         addr = radeon_bo_gpu_offset(bo);
602         ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
603         ib.ptr[1] = addr;
604         ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
605         ib.ptr[3] = addr >> 32;
606         ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
607         ib.ptr[5] = 0;
608         for (i = 6; i < 16; ++i)
609                 ib.ptr[i] = PACKET2(0);
610         ib.length_dw = 16;
611
612         r = radeon_ib_schedule(rdev, &ib, NULL);
613         if (r) {
614                 ttm_eu_backoff_reservation(&head);
615                 return r;
616         }
617         ttm_eu_fence_buffer_objects(&head, ib.fence);
618
619         if (fence)
620                 *fence = radeon_fence_ref(ib.fence);
621
622         radeon_ib_free(rdev, &ib);
623         radeon_bo_unref(&bo);
624         return 0;
625 }
626
627 /* multiple fence commands without any stream commands in between can
628    crash the vcpu so just try to emmit a dummy create/destroy msg to
629    avoid this */
630 int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
631                               uint32_t handle, struct radeon_fence **fence)
632 {
633         struct radeon_bo *bo;
634         uint32_t *msg;
635         int r, i;
636
637         r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
638                              RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
639         if (r)
640                 return r;
641
642         r = radeon_bo_reserve(bo, false);
643         if (r) {
644                 radeon_bo_unref(&bo);
645                 return r;
646         }
647
648         r = radeon_bo_kmap(bo, (void **)&msg);
649         if (r) {
650                 radeon_bo_unreserve(bo);
651                 radeon_bo_unref(&bo);
652                 return r;
653         }
654
655         /* stitch together an UVD create msg */
656         msg[0] = cpu_to_le32(0x00000de4);
657         msg[1] = cpu_to_le32(0x00000000);
658         msg[2] = cpu_to_le32(handle);
659         msg[3] = cpu_to_le32(0x00000000);
660         msg[4] = cpu_to_le32(0x00000000);
661         msg[5] = cpu_to_le32(0x00000000);
662         msg[6] = cpu_to_le32(0x00000000);
663         msg[7] = cpu_to_le32(0x00000780);
664         msg[8] = cpu_to_le32(0x00000440);
665         msg[9] = cpu_to_le32(0x00000000);
666         msg[10] = cpu_to_le32(0x01b37000);
667         for (i = 11; i < 1024; ++i)
668                 msg[i] = cpu_to_le32(0x0);
669
670         radeon_bo_kunmap(bo);
671         radeon_bo_unreserve(bo);
672
673         return radeon_uvd_send_msg(rdev, ring, bo, fence);
674 }
675
676 int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
677                                uint32_t handle, struct radeon_fence **fence)
678 {
679         struct radeon_bo *bo;
680         uint32_t *msg;
681         int r, i;
682
683         r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
684                              RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
685         if (r)
686                 return r;
687
688         r = radeon_bo_reserve(bo, false);
689         if (r) {
690                 radeon_bo_unref(&bo);
691                 return r;
692         }
693
694         r = radeon_bo_kmap(bo, (void **)&msg);
695         if (r) {
696                 radeon_bo_unreserve(bo);
697                 radeon_bo_unref(&bo);
698                 return r;
699         }
700
701         /* stitch together an UVD destroy msg */
702         msg[0] = cpu_to_le32(0x00000de4);
703         msg[1] = cpu_to_le32(0x00000002);
704         msg[2] = cpu_to_le32(handle);
705         msg[3] = cpu_to_le32(0x00000000);
706         for (i = 4; i < 1024; ++i)
707                 msg[i] = cpu_to_le32(0x0);
708
709         radeon_bo_kunmap(bo);
710         radeon_bo_unreserve(bo);
711
712         return radeon_uvd_send_msg(rdev, ring, bo, fence);
713 }
714
715 static void radeon_uvd_idle_work_handler(struct work_struct *work)
716 {
717         struct radeon_device *rdev =
718                 container_of(work, struct radeon_device, uvd.idle_work.work);
719
720         if (radeon_fence_count_emitted(rdev, R600_RING_TYPE_UVD_INDEX) == 0)
721                 radeon_set_uvd_clocks(rdev, 0, 0);
722         else
723                 schedule_delayed_work(&rdev->uvd.idle_work,
724                                       msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
725 }
726
727 void radeon_uvd_note_usage(struct radeon_device *rdev)
728 {
729         bool set_clocks = !cancel_delayed_work_sync(&rdev->uvd.idle_work);
730         set_clocks &= schedule_delayed_work(&rdev->uvd.idle_work,
731                                             msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
732         if (set_clocks)
733                 radeon_set_uvd_clocks(rdev, 53300, 40000);
734 }
735
736 static unsigned radeon_uvd_calc_upll_post_div(unsigned vco_freq,
737                                               unsigned target_freq,
738                                               unsigned pd_min,
739                                               unsigned pd_even)
740 {
741         unsigned post_div = vco_freq / target_freq;
742
743         /* adjust to post divider minimum value */
744         if (post_div < pd_min)
745                 post_div = pd_min;
746
747         /* we alway need a frequency less than or equal the target */
748         if ((vco_freq / post_div) > target_freq)
749                 post_div += 1;
750
751         /* post dividers above a certain value must be even */
752         if (post_div > pd_even && post_div % 2)
753                 post_div += 1;
754
755         return post_div;
756 }
757
758 /**
759  * radeon_uvd_calc_upll_dividers - calc UPLL clock dividers
760  *
761  * @rdev: radeon_device pointer
762  * @vclk: wanted VCLK
763  * @dclk: wanted DCLK
764  * @vco_min: minimum VCO frequency
765  * @vco_max: maximum VCO frequency
766  * @fb_factor: factor to multiply vco freq with
767  * @fb_mask: limit and bitmask for feedback divider
768  * @pd_min: post divider minimum
769  * @pd_max: post divider maximum
770  * @pd_even: post divider must be even above this value
771  * @optimal_fb_div: resulting feedback divider
772  * @optimal_vclk_div: resulting vclk post divider
773  * @optimal_dclk_div: resulting dclk post divider
774  *
775  * Calculate dividers for UVDs UPLL (R6xx-SI, except APUs).
776  * Returns zero on success -EINVAL on error.
777  */
778 int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
779                                   unsigned vclk, unsigned dclk,
780                                   unsigned vco_min, unsigned vco_max,
781                                   unsigned fb_factor, unsigned fb_mask,
782                                   unsigned pd_min, unsigned pd_max,
783                                   unsigned pd_even,
784                                   unsigned *optimal_fb_div,
785                                   unsigned *optimal_vclk_div,
786                                   unsigned *optimal_dclk_div)
787 {
788         unsigned vco_freq, ref_freq = rdev->clock.spll.reference_freq;
789
790         /* start off with something large */
791         unsigned optimal_score = ~0;
792
793         /* loop through vco from low to high */
794         vco_min = max(max(vco_min, vclk), dclk);
795         for (vco_freq = vco_min; vco_freq <= vco_max; vco_freq += 100) {
796
797                 uint64_t fb_div = (uint64_t)vco_freq * fb_factor;
798                 unsigned vclk_div, dclk_div, score;
799
800                 do_div(fb_div, ref_freq);
801
802                 /* fb div out of range ? */
803                 if (fb_div > fb_mask)
804                         break; /* it can oly get worse */
805
806                 fb_div &= fb_mask;
807
808                 /* calc vclk divider with current vco freq */
809                 vclk_div = radeon_uvd_calc_upll_post_div(vco_freq, vclk,
810                                                          pd_min, pd_even);
811                 if (vclk_div > pd_max)
812                         break; /* vco is too big, it has to stop */
813
814                 /* calc dclk divider with current vco freq */
815                 dclk_div = radeon_uvd_calc_upll_post_div(vco_freq, dclk,
816                                                          pd_min, pd_even);
817                 if (vclk_div > pd_max)
818                         break; /* vco is too big, it has to stop */
819
820                 /* calc score with current vco freq */
821                 score = vclk - (vco_freq / vclk_div) + dclk - (vco_freq / dclk_div);
822
823                 /* determine if this vco setting is better than current optimal settings */
824                 if (score < optimal_score) {
825                         *optimal_fb_div = fb_div;
826                         *optimal_vclk_div = vclk_div;
827                         *optimal_dclk_div = dclk_div;
828                         optimal_score = score;
829                         if (optimal_score == 0)
830                                 break; /* it can't get better than this */
831                 }
832         }
833
834         /* did we found a valid setup ? */
835         if (optimal_score == ~0)
836                 return -EINVAL;
837
838         return 0;
839 }
840
841 int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
842                                 unsigned cg_upll_func_cntl)
843 {
844         unsigned i;
845
846         /* make sure UPLL_CTLREQ is deasserted */
847         WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
848
849         mdelay(10);
850
851         /* assert UPLL_CTLREQ */
852         WREG32_P(cg_upll_func_cntl, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
853
854         /* wait for CTLACK and CTLACK2 to get asserted */
855         for (i = 0; i < 100; ++i) {
856                 uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
857                 if ((RREG32(cg_upll_func_cntl) & mask) == mask)
858                         break;
859                 mdelay(10);
860         }
861
862         /* deassert UPLL_CTLREQ */
863         WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
864
865         if (i == 100) {
866                 DRM_ERROR("Timeout setting UVD clocks!\n");
867                 return -ETIMEDOUT;
868         }
869
870         return 0;
871 }