i915_gem.c 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/i915_drm.h>
  29. #include "i915_drv.h"
  30. #include "i915_trace.h"
  31. #include "intel_drv.h"
  32. #include <linux/shmem_fs.h>
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. #include <linux/dma-buf.h>
  37. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  38. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  39. static __must_check int
  40. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  41. struct i915_address_space *vm,
  42. unsigned alignment,
  43. bool map_and_fenceable,
  44. bool nonblocking);
  45. static int i915_gem_phys_pwrite(struct drm_device *dev,
  46. struct drm_i915_gem_object *obj,
  47. struct drm_i915_gem_pwrite *args,
  48. struct drm_file *file);
  49. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  50. struct drm_i915_gem_object *obj);
  51. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  52. struct drm_i915_fence_reg *fence,
  53. bool enable);
  54. static int i915_gem_inactive_shrink(struct shrinker *shrinker,
  55. struct shrink_control *sc);
  56. static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
  57. static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
  58. static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
  59. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  60. {
  61. if (obj->tiling_mode)
  62. i915_gem_release_mmap(obj);
  63. /* As we do not have an associated fence register, we will force
  64. * a tiling change if we ever need to acquire one.
  65. */
  66. obj->fence_dirty = false;
  67. obj->fence_reg = I915_FENCE_REG_NONE;
  68. }
  69. /* some bookkeeping */
  70. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  71. size_t size)
  72. {
  73. spin_lock(&dev_priv->mm.object_stat_lock);
  74. dev_priv->mm.object_count++;
  75. dev_priv->mm.object_memory += size;
  76. spin_unlock(&dev_priv->mm.object_stat_lock);
  77. }
  78. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  79. size_t size)
  80. {
  81. spin_lock(&dev_priv->mm.object_stat_lock);
  82. dev_priv->mm.object_count--;
  83. dev_priv->mm.object_memory -= size;
  84. spin_unlock(&dev_priv->mm.object_stat_lock);
  85. }
  86. static int
  87. i915_gem_wait_for_error(struct i915_gpu_error *error)
  88. {
  89. int ret;
  90. #define EXIT_COND (!i915_reset_in_progress(error) || \
  91. i915_terminally_wedged(error))
  92. if (EXIT_COND)
  93. return 0;
  94. /*
  95. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  96. * userspace. If it takes that long something really bad is going on and
  97. * we should simply try to bail out and fail as gracefully as possible.
  98. */
  99. ret = wait_event_interruptible_timeout(error->reset_queue,
  100. EXIT_COND,
  101. 10*HZ);
  102. if (ret == 0) {
  103. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  104. return -EIO;
  105. } else if (ret < 0) {
  106. return ret;
  107. }
  108. #undef EXIT_COND
  109. return 0;
  110. }
  111. int i915_mutex_lock_interruptible(struct drm_device *dev)
  112. {
  113. struct drm_i915_private *dev_priv = dev->dev_private;
  114. int ret;
  115. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  116. if (ret)
  117. return ret;
  118. ret = mutex_lock_interruptible(&dev->struct_mutex);
  119. if (ret)
  120. return ret;
  121. WARN_ON(i915_verify_lists(dev));
  122. return 0;
  123. }
  124. static inline bool
  125. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  126. {
  127. return i915_gem_obj_bound_any(obj) && !obj->active;
  128. }
  129. int
  130. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  131. struct drm_file *file)
  132. {
  133. struct drm_i915_private *dev_priv = dev->dev_private;
  134. struct drm_i915_gem_init *args = data;
  135. if (drm_core_check_feature(dev, DRIVER_MODESET))
  136. return -ENODEV;
  137. if (args->gtt_start >= args->gtt_end ||
  138. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  139. return -EINVAL;
  140. /* GEM with user mode setting was never supported on ilk and later. */
  141. if (INTEL_INFO(dev)->gen >= 5)
  142. return -ENODEV;
  143. mutex_lock(&dev->struct_mutex);
  144. i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
  145. args->gtt_end);
  146. dev_priv->gtt.mappable_end = args->gtt_end;
  147. mutex_unlock(&dev->struct_mutex);
  148. return 0;
  149. }
  150. int
  151. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  152. struct drm_file *file)
  153. {
  154. struct drm_i915_private *dev_priv = dev->dev_private;
  155. struct drm_i915_gem_get_aperture *args = data;
  156. struct drm_i915_gem_object *obj;
  157. size_t pinned;
  158. pinned = 0;
  159. mutex_lock(&dev->struct_mutex);
  160. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  161. if (obj->pin_count)
  162. pinned += i915_gem_obj_ggtt_size(obj);
  163. mutex_unlock(&dev->struct_mutex);
  164. args->aper_size = dev_priv->gtt.base.total;
  165. args->aper_available_size = args->aper_size - pinned;
  166. return 0;
  167. }
  168. void *i915_gem_object_alloc(struct drm_device *dev)
  169. {
  170. struct drm_i915_private *dev_priv = dev->dev_private;
  171. return kmem_cache_alloc(dev_priv->slab, GFP_KERNEL | __GFP_ZERO);
  172. }
  173. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  174. {
  175. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  176. kmem_cache_free(dev_priv->slab, obj);
  177. }
  178. static int
  179. i915_gem_create(struct drm_file *file,
  180. struct drm_device *dev,
  181. uint64_t size,
  182. uint32_t *handle_p)
  183. {
  184. struct drm_i915_gem_object *obj;
  185. int ret;
  186. u32 handle;
  187. size = roundup(size, PAGE_SIZE);
  188. if (size == 0)
  189. return -EINVAL;
  190. /* Allocate the new object */
  191. obj = i915_gem_alloc_object(dev, size);
  192. if (obj == NULL)
  193. return -ENOMEM;
  194. ret = drm_gem_handle_create(file, &obj->base, &handle);
  195. /* drop reference from allocate - handle holds it now */
  196. drm_gem_object_unreference_unlocked(&obj->base);
  197. if (ret)
  198. return ret;
  199. *handle_p = handle;
  200. return 0;
  201. }
  202. int
  203. i915_gem_dumb_create(struct drm_file *file,
  204. struct drm_device *dev,
  205. struct drm_mode_create_dumb *args)
  206. {
  207. /* have to work out size/pitch and return them */
  208. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  209. args->size = args->pitch * args->height;
  210. return i915_gem_create(file, dev,
  211. args->size, &args->handle);
  212. }
  213. int i915_gem_dumb_destroy(struct drm_file *file,
  214. struct drm_device *dev,
  215. uint32_t handle)
  216. {
  217. return drm_gem_handle_delete(file, handle);
  218. }
  219. /**
  220. * Creates a new mm object and returns a handle to it.
  221. */
  222. int
  223. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  224. struct drm_file *file)
  225. {
  226. struct drm_i915_gem_create *args = data;
  227. return i915_gem_create(file, dev,
  228. args->size, &args->handle);
  229. }
  230. static inline int
  231. __copy_to_user_swizzled(char __user *cpu_vaddr,
  232. const char *gpu_vaddr, int gpu_offset,
  233. int length)
  234. {
  235. int ret, cpu_offset = 0;
  236. while (length > 0) {
  237. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  238. int this_length = min(cacheline_end - gpu_offset, length);
  239. int swizzled_gpu_offset = gpu_offset ^ 64;
  240. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  241. gpu_vaddr + swizzled_gpu_offset,
  242. this_length);
  243. if (ret)
  244. return ret + length;
  245. cpu_offset += this_length;
  246. gpu_offset += this_length;
  247. length -= this_length;
  248. }
  249. return 0;
  250. }
  251. static inline int
  252. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  253. const char __user *cpu_vaddr,
  254. int length)
  255. {
  256. int ret, cpu_offset = 0;
  257. while (length > 0) {
  258. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  259. int this_length = min(cacheline_end - gpu_offset, length);
  260. int swizzled_gpu_offset = gpu_offset ^ 64;
  261. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  262. cpu_vaddr + cpu_offset,
  263. this_length);
  264. if (ret)
  265. return ret + length;
  266. cpu_offset += this_length;
  267. gpu_offset += this_length;
  268. length -= this_length;
  269. }
  270. return 0;
  271. }
  272. /* Per-page copy function for the shmem pread fastpath.
  273. * Flushes invalid cachelines before reading the target if
  274. * needs_clflush is set. */
  275. static int
  276. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  277. char __user *user_data,
  278. bool page_do_bit17_swizzling, bool needs_clflush)
  279. {
  280. char *vaddr;
  281. int ret;
  282. if (unlikely(page_do_bit17_swizzling))
  283. return -EINVAL;
  284. vaddr = kmap_atomic(page);
  285. if (needs_clflush)
  286. drm_clflush_virt_range(vaddr + shmem_page_offset,
  287. page_length);
  288. ret = __copy_to_user_inatomic(user_data,
  289. vaddr + shmem_page_offset,
  290. page_length);
  291. kunmap_atomic(vaddr);
  292. return ret ? -EFAULT : 0;
  293. }
  294. static void
  295. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  296. bool swizzled)
  297. {
  298. if (unlikely(swizzled)) {
  299. unsigned long start = (unsigned long) addr;
  300. unsigned long end = (unsigned long) addr + length;
  301. /* For swizzling simply ensure that we always flush both
  302. * channels. Lame, but simple and it works. Swizzled
  303. * pwrite/pread is far from a hotpath - current userspace
  304. * doesn't use it at all. */
  305. start = round_down(start, 128);
  306. end = round_up(end, 128);
  307. drm_clflush_virt_range((void *)start, end - start);
  308. } else {
  309. drm_clflush_virt_range(addr, length);
  310. }
  311. }
  312. /* Only difference to the fast-path function is that this can handle bit17
  313. * and uses non-atomic copy and kmap functions. */
  314. static int
  315. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  316. char __user *user_data,
  317. bool page_do_bit17_swizzling, bool needs_clflush)
  318. {
  319. char *vaddr;
  320. int ret;
  321. vaddr = kmap(page);
  322. if (needs_clflush)
  323. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  324. page_length,
  325. page_do_bit17_swizzling);
  326. if (page_do_bit17_swizzling)
  327. ret = __copy_to_user_swizzled(user_data,
  328. vaddr, shmem_page_offset,
  329. page_length);
  330. else
  331. ret = __copy_to_user(user_data,
  332. vaddr + shmem_page_offset,
  333. page_length);
  334. kunmap(page);
  335. return ret ? - EFAULT : 0;
  336. }
  337. static int
  338. i915_gem_shmem_pread(struct drm_device *dev,
  339. struct drm_i915_gem_object *obj,
  340. struct drm_i915_gem_pread *args,
  341. struct drm_file *file)
  342. {
  343. char __user *user_data;
  344. ssize_t remain;
  345. loff_t offset;
  346. int shmem_page_offset, page_length, ret = 0;
  347. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  348. int prefaulted = 0;
  349. int needs_clflush = 0;
  350. struct sg_page_iter sg_iter;
  351. user_data = to_user_ptr(args->data_ptr);
  352. remain = args->size;
  353. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  354. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  355. /* If we're not in the cpu read domain, set ourself into the gtt
  356. * read domain and manually flush cachelines (if required). This
  357. * optimizes for the case when the gpu will dirty the data
  358. * anyway again before the next pread happens. */
  359. if (obj->cache_level == I915_CACHE_NONE)
  360. needs_clflush = 1;
  361. if (i915_gem_obj_bound_any(obj)) {
  362. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  363. if (ret)
  364. return ret;
  365. }
  366. }
  367. ret = i915_gem_object_get_pages(obj);
  368. if (ret)
  369. return ret;
  370. i915_gem_object_pin_pages(obj);
  371. offset = args->offset;
  372. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  373. offset >> PAGE_SHIFT) {
  374. struct page *page = sg_page_iter_page(&sg_iter);
  375. if (remain <= 0)
  376. break;
  377. /* Operation in this page
  378. *
  379. * shmem_page_offset = offset within page in shmem file
  380. * page_length = bytes to copy for this page
  381. */
  382. shmem_page_offset = offset_in_page(offset);
  383. page_length = remain;
  384. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  385. page_length = PAGE_SIZE - shmem_page_offset;
  386. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  387. (page_to_phys(page) & (1 << 17)) != 0;
  388. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  389. user_data, page_do_bit17_swizzling,
  390. needs_clflush);
  391. if (ret == 0)
  392. goto next_page;
  393. mutex_unlock(&dev->struct_mutex);
  394. if (likely(!i915_prefault_disable) && !prefaulted) {
  395. ret = fault_in_multipages_writeable(user_data, remain);
  396. /* Userspace is tricking us, but we've already clobbered
  397. * its pages with the prefault and promised to write the
  398. * data up to the first fault. Hence ignore any errors
  399. * and just continue. */
  400. (void)ret;
  401. prefaulted = 1;
  402. }
  403. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  404. user_data, page_do_bit17_swizzling,
  405. needs_clflush);
  406. mutex_lock(&dev->struct_mutex);
  407. next_page:
  408. mark_page_accessed(page);
  409. if (ret)
  410. goto out;
  411. remain -= page_length;
  412. user_data += page_length;
  413. offset += page_length;
  414. }
  415. out:
  416. i915_gem_object_unpin_pages(obj);
  417. return ret;
  418. }
  419. /**
  420. * Reads data from the object referenced by handle.
  421. *
  422. * On error, the contents of *data are undefined.
  423. */
  424. int
  425. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  426. struct drm_file *file)
  427. {
  428. struct drm_i915_gem_pread *args = data;
  429. struct drm_i915_gem_object *obj;
  430. int ret = 0;
  431. if (args->size == 0)
  432. return 0;
  433. if (!access_ok(VERIFY_WRITE,
  434. to_user_ptr(args->data_ptr),
  435. args->size))
  436. return -EFAULT;
  437. ret = i915_mutex_lock_interruptible(dev);
  438. if (ret)
  439. return ret;
  440. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  441. if (&obj->base == NULL) {
  442. ret = -ENOENT;
  443. goto unlock;
  444. }
  445. /* Bounds check source. */
  446. if (args->offset > obj->base.size ||
  447. args->size > obj->base.size - args->offset) {
  448. ret = -EINVAL;
  449. goto out;
  450. }
  451. /* prime objects have no backing filp to GEM pread/pwrite
  452. * pages from.
  453. */
  454. if (!obj->base.filp) {
  455. ret = -EINVAL;
  456. goto out;
  457. }
  458. trace_i915_gem_object_pread(obj, args->offset, args->size);
  459. ret = i915_gem_shmem_pread(dev, obj, args, file);
  460. out:
  461. drm_gem_object_unreference(&obj->base);
  462. unlock:
  463. mutex_unlock(&dev->struct_mutex);
  464. return ret;
  465. }
  466. /* This is the fast write path which cannot handle
  467. * page faults in the source data
  468. */
  469. static inline int
  470. fast_user_write(struct io_mapping *mapping,
  471. loff_t page_base, int page_offset,
  472. char __user *user_data,
  473. int length)
  474. {
  475. void __iomem *vaddr_atomic;
  476. void *vaddr;
  477. unsigned long unwritten;
  478. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  479. /* We can use the cpu mem copy function because this is X86. */
  480. vaddr = (void __force*)vaddr_atomic + page_offset;
  481. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  482. user_data, length);
  483. io_mapping_unmap_atomic(vaddr_atomic);
  484. return unwritten;
  485. }
  486. /**
  487. * This is the fast pwrite path, where we copy the data directly from the
  488. * user into the GTT, uncached.
  489. */
  490. static int
  491. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  492. struct drm_i915_gem_object *obj,
  493. struct drm_i915_gem_pwrite *args,
  494. struct drm_file *file)
  495. {
  496. drm_i915_private_t *dev_priv = dev->dev_private;
  497. ssize_t remain;
  498. loff_t offset, page_base;
  499. char __user *user_data;
  500. int page_offset, page_length, ret;
  501. ret = i915_gem_obj_ggtt_pin(obj, 0, true, true);
  502. if (ret)
  503. goto out;
  504. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  505. if (ret)
  506. goto out_unpin;
  507. ret = i915_gem_object_put_fence(obj);
  508. if (ret)
  509. goto out_unpin;
  510. user_data = to_user_ptr(args->data_ptr);
  511. remain = args->size;
  512. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  513. while (remain > 0) {
  514. /* Operation in this page
  515. *
  516. * page_base = page offset within aperture
  517. * page_offset = offset within page
  518. * page_length = bytes to copy for this page
  519. */
  520. page_base = offset & PAGE_MASK;
  521. page_offset = offset_in_page(offset);
  522. page_length = remain;
  523. if ((page_offset + remain) > PAGE_SIZE)
  524. page_length = PAGE_SIZE - page_offset;
  525. /* If we get a fault while copying data, then (presumably) our
  526. * source page isn't available. Return the error and we'll
  527. * retry in the slow path.
  528. */
  529. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  530. page_offset, user_data, page_length)) {
  531. ret = -EFAULT;
  532. goto out_unpin;
  533. }
  534. remain -= page_length;
  535. user_data += page_length;
  536. offset += page_length;
  537. }
  538. out_unpin:
  539. i915_gem_object_unpin(obj);
  540. out:
  541. return ret;
  542. }
  543. /* Per-page copy function for the shmem pwrite fastpath.
  544. * Flushes invalid cachelines before writing to the target if
  545. * needs_clflush_before is set and flushes out any written cachelines after
  546. * writing if needs_clflush is set. */
  547. static int
  548. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  549. char __user *user_data,
  550. bool page_do_bit17_swizzling,
  551. bool needs_clflush_before,
  552. bool needs_clflush_after)
  553. {
  554. char *vaddr;
  555. int ret;
  556. if (unlikely(page_do_bit17_swizzling))
  557. return -EINVAL;
  558. vaddr = kmap_atomic(page);
  559. if (needs_clflush_before)
  560. drm_clflush_virt_range(vaddr + shmem_page_offset,
  561. page_length);
  562. ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
  563. user_data,
  564. page_length);
  565. if (needs_clflush_after)
  566. drm_clflush_virt_range(vaddr + shmem_page_offset,
  567. page_length);
  568. kunmap_atomic(vaddr);
  569. return ret ? -EFAULT : 0;
  570. }
  571. /* Only difference to the fast-path function is that this can handle bit17
  572. * and uses non-atomic copy and kmap functions. */
  573. static int
  574. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  575. char __user *user_data,
  576. bool page_do_bit17_swizzling,
  577. bool needs_clflush_before,
  578. bool needs_clflush_after)
  579. {
  580. char *vaddr;
  581. int ret;
  582. vaddr = kmap(page);
  583. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  584. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  585. page_length,
  586. page_do_bit17_swizzling);
  587. if (page_do_bit17_swizzling)
  588. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  589. user_data,
  590. page_length);
  591. else
  592. ret = __copy_from_user(vaddr + shmem_page_offset,
  593. user_data,
  594. page_length);
  595. if (needs_clflush_after)
  596. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  597. page_length,
  598. page_do_bit17_swizzling);
  599. kunmap(page);
  600. return ret ? -EFAULT : 0;
  601. }
  602. static int
  603. i915_gem_shmem_pwrite(struct drm_device *dev,
  604. struct drm_i915_gem_object *obj,
  605. struct drm_i915_gem_pwrite *args,
  606. struct drm_file *file)
  607. {
  608. ssize_t remain;
  609. loff_t offset;
  610. char __user *user_data;
  611. int shmem_page_offset, page_length, ret = 0;
  612. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  613. int hit_slowpath = 0;
  614. int needs_clflush_after = 0;
  615. int needs_clflush_before = 0;
  616. struct sg_page_iter sg_iter;
  617. user_data = to_user_ptr(args->data_ptr);
  618. remain = args->size;
  619. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  620. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  621. /* If we're not in the cpu write domain, set ourself into the gtt
  622. * write domain and manually flush cachelines (if required). This
  623. * optimizes for the case when the gpu will use the data
  624. * right away and we therefore have to clflush anyway. */
  625. if (obj->cache_level == I915_CACHE_NONE)
  626. needs_clflush_after = 1;
  627. if (i915_gem_obj_bound_any(obj)) {
  628. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  629. if (ret)
  630. return ret;
  631. }
  632. }
  633. /* Same trick applies for invalidate partially written cachelines before
  634. * writing. */
  635. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
  636. && obj->cache_level == I915_CACHE_NONE)
  637. needs_clflush_before = 1;
  638. ret = i915_gem_object_get_pages(obj);
  639. if (ret)
  640. return ret;
  641. i915_gem_object_pin_pages(obj);
  642. offset = args->offset;
  643. obj->dirty = 1;
  644. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  645. offset >> PAGE_SHIFT) {
  646. struct page *page = sg_page_iter_page(&sg_iter);
  647. int partial_cacheline_write;
  648. if (remain <= 0)
  649. break;
  650. /* Operation in this page
  651. *
  652. * shmem_page_offset = offset within page in shmem file
  653. * page_length = bytes to copy for this page
  654. */
  655. shmem_page_offset = offset_in_page(offset);
  656. page_length = remain;
  657. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  658. page_length = PAGE_SIZE - shmem_page_offset;
  659. /* If we don't overwrite a cacheline completely we need to be
  660. * careful to have up-to-date data by first clflushing. Don't
  661. * overcomplicate things and flush the entire patch. */
  662. partial_cacheline_write = needs_clflush_before &&
  663. ((shmem_page_offset | page_length)
  664. & (boot_cpu_data.x86_clflush_size - 1));
  665. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  666. (page_to_phys(page) & (1 << 17)) != 0;
  667. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  668. user_data, page_do_bit17_swizzling,
  669. partial_cacheline_write,
  670. needs_clflush_after);
  671. if (ret == 0)
  672. goto next_page;
  673. hit_slowpath = 1;
  674. mutex_unlock(&dev->struct_mutex);
  675. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  676. user_data, page_do_bit17_swizzling,
  677. partial_cacheline_write,
  678. needs_clflush_after);
  679. mutex_lock(&dev->struct_mutex);
  680. next_page:
  681. set_page_dirty(page);
  682. mark_page_accessed(page);
  683. if (ret)
  684. goto out;
  685. remain -= page_length;
  686. user_data += page_length;
  687. offset += page_length;
  688. }
  689. out:
  690. i915_gem_object_unpin_pages(obj);
  691. if (hit_slowpath) {
  692. /*
  693. * Fixup: Flush cpu caches in case we didn't flush the dirty
  694. * cachelines in-line while writing and the object moved
  695. * out of the cpu write domain while we've dropped the lock.
  696. */
  697. if (!needs_clflush_after &&
  698. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  699. i915_gem_clflush_object(obj);
  700. i915_gem_chipset_flush(dev);
  701. }
  702. }
  703. if (needs_clflush_after)
  704. i915_gem_chipset_flush(dev);
  705. return ret;
  706. }
  707. /**
  708. * Writes data to the object referenced by handle.
  709. *
  710. * On error, the contents of the buffer that were to be modified are undefined.
  711. */
  712. int
  713. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  714. struct drm_file *file)
  715. {
  716. struct drm_i915_gem_pwrite *args = data;
  717. struct drm_i915_gem_object *obj;
  718. int ret;
  719. if (args->size == 0)
  720. return 0;
  721. if (!access_ok(VERIFY_READ,
  722. to_user_ptr(args->data_ptr),
  723. args->size))
  724. return -EFAULT;
  725. if (likely(!i915_prefault_disable)) {
  726. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  727. args->size);
  728. if (ret)
  729. return -EFAULT;
  730. }
  731. ret = i915_mutex_lock_interruptible(dev);
  732. if (ret)
  733. return ret;
  734. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  735. if (&obj->base == NULL) {
  736. ret = -ENOENT;
  737. goto unlock;
  738. }
  739. /* Bounds check destination. */
  740. if (args->offset > obj->base.size ||
  741. args->size > obj->base.size - args->offset) {
  742. ret = -EINVAL;
  743. goto out;
  744. }
  745. /* prime objects have no backing filp to GEM pread/pwrite
  746. * pages from.
  747. */
  748. if (!obj->base.filp) {
  749. ret = -EINVAL;
  750. goto out;
  751. }
  752. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  753. ret = -EFAULT;
  754. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  755. * it would end up going through the fenced access, and we'll get
  756. * different detiling behavior between reading and writing.
  757. * pread/pwrite currently are reading and writing from the CPU
  758. * perspective, requiring manual detiling by the client.
  759. */
  760. if (obj->phys_obj) {
  761. ret = i915_gem_phys_pwrite(dev, obj, args, file);
  762. goto out;
  763. }
  764. if (obj->cache_level == I915_CACHE_NONE &&
  765. obj->tiling_mode == I915_TILING_NONE &&
  766. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  767. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  768. /* Note that the gtt paths might fail with non-page-backed user
  769. * pointers (e.g. gtt mappings when moving data between
  770. * textures). Fallback to the shmem path in that case. */
  771. }
  772. if (ret == -EFAULT || ret == -ENOSPC)
  773. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  774. out:
  775. drm_gem_object_unreference(&obj->base);
  776. unlock:
  777. mutex_unlock(&dev->struct_mutex);
  778. return ret;
  779. }
  780. int
  781. i915_gem_check_wedge(struct i915_gpu_error *error,
  782. bool interruptible)
  783. {
  784. if (i915_reset_in_progress(error)) {
  785. /* Non-interruptible callers can't handle -EAGAIN, hence return
  786. * -EIO unconditionally for these. */
  787. if (!interruptible)
  788. return -EIO;
  789. /* Recovery complete, but the reset failed ... */
  790. if (i915_terminally_wedged(error))
  791. return -EIO;
  792. return -EAGAIN;
  793. }
  794. return 0;
  795. }
  796. /*
  797. * Compare seqno against outstanding lazy request. Emit a request if they are
  798. * equal.
  799. */
  800. static int
  801. i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
  802. {
  803. int ret;
  804. BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  805. ret = 0;
  806. if (seqno == ring->outstanding_lazy_request)
  807. ret = i915_add_request(ring, NULL);
  808. return ret;
  809. }
  810. /**
  811. * __wait_seqno - wait until execution of seqno has finished
  812. * @ring: the ring expected to report seqno
  813. * @seqno: duh!
  814. * @reset_counter: reset sequence associated with the given seqno
  815. * @interruptible: do an interruptible wait (normally yes)
  816. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  817. *
  818. * Note: It is of utmost importance that the passed in seqno and reset_counter
  819. * values have been read by the caller in an smp safe manner. Where read-side
  820. * locks are involved, it is sufficient to read the reset_counter before
  821. * unlocking the lock that protects the seqno. For lockless tricks, the
  822. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  823. * inserted.
  824. *
  825. * Returns 0 if the seqno was found within the alloted time. Else returns the
  826. * errno with remaining time filled in timeout argument.
  827. */
  828. static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
  829. unsigned reset_counter,
  830. bool interruptible, struct timespec *timeout)
  831. {
  832. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  833. struct timespec before, now, wait_time={1,0};
  834. unsigned long timeout_jiffies;
  835. long end;
  836. bool wait_forever = true;
  837. int ret;
  838. if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
  839. return 0;
  840. trace_i915_gem_request_wait_begin(ring, seqno);
  841. if (timeout != NULL) {
  842. wait_time = *timeout;
  843. wait_forever = false;
  844. }
  845. timeout_jiffies = timespec_to_jiffies_timeout(&wait_time);
  846. if (WARN_ON(!ring->irq_get(ring)))
  847. return -ENODEV;
  848. /* Record current time in case interrupted by signal, or wedged * */
  849. getrawmonotonic(&before);
  850. #define EXIT_COND \
  851. (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
  852. i915_reset_in_progress(&dev_priv->gpu_error) || \
  853. reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  854. do {
  855. if (interruptible)
  856. end = wait_event_interruptible_timeout(ring->irq_queue,
  857. EXIT_COND,
  858. timeout_jiffies);
  859. else
  860. end = wait_event_timeout(ring->irq_queue, EXIT_COND,
  861. timeout_jiffies);
  862. /* We need to check whether any gpu reset happened in between
  863. * the caller grabbing the seqno and now ... */
  864. if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  865. end = -EAGAIN;
  866. /* ... but upgrade the -EGAIN to an -EIO if the gpu is truely
  867. * gone. */
  868. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  869. if (ret)
  870. end = ret;
  871. } while (end == 0 && wait_forever);
  872. getrawmonotonic(&now);
  873. ring->irq_put(ring);
  874. trace_i915_gem_request_wait_end(ring, seqno);
  875. #undef EXIT_COND
  876. if (timeout) {
  877. struct timespec sleep_time = timespec_sub(now, before);
  878. *timeout = timespec_sub(*timeout, sleep_time);
  879. if (!timespec_valid(timeout)) /* i.e. negative time remains */
  880. set_normalized_timespec(timeout, 0, 0);
  881. }
  882. switch (end) {
  883. case -EIO:
  884. case -EAGAIN: /* Wedged */
  885. case -ERESTARTSYS: /* Signal */
  886. return (int)end;
  887. case 0: /* Timeout */
  888. return -ETIME;
  889. default: /* Completed */
  890. WARN_ON(end < 0); /* We're not aware of other errors */
  891. return 0;
  892. }
  893. }
  894. /**
  895. * Waits for a sequence number to be signaled, and cleans up the
  896. * request and object lists appropriately for that event.
  897. */
  898. int
  899. i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
  900. {
  901. struct drm_device *dev = ring->dev;
  902. struct drm_i915_private *dev_priv = dev->dev_private;
  903. bool interruptible = dev_priv->mm.interruptible;
  904. int ret;
  905. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  906. BUG_ON(seqno == 0);
  907. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  908. if (ret)
  909. return ret;
  910. ret = i915_gem_check_olr(ring, seqno);
  911. if (ret)
  912. return ret;
  913. return __wait_seqno(ring, seqno,
  914. atomic_read(&dev_priv->gpu_error.reset_counter),
  915. interruptible, NULL);
  916. }
  917. static int
  918. i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj,
  919. struct intel_ring_buffer *ring)
  920. {
  921. i915_gem_retire_requests_ring(ring);
  922. /* Manually manage the write flush as we may have not yet
  923. * retired the buffer.
  924. *
  925. * Note that the last_write_seqno is always the earlier of
  926. * the two (read/write) seqno, so if we haved successfully waited,
  927. * we know we have passed the last write.
  928. */
  929. obj->last_write_seqno = 0;
  930. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  931. return 0;
  932. }
  933. /**
  934. * Ensures that all rendering to the object has completed and the object is
  935. * safe to unbind from the GTT or access from the CPU.
  936. */
  937. static __must_check int
  938. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  939. bool readonly)
  940. {
  941. struct intel_ring_buffer *ring = obj->ring;
  942. u32 seqno;
  943. int ret;
  944. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  945. if (seqno == 0)
  946. return 0;
  947. ret = i915_wait_seqno(ring, seqno);
  948. if (ret)
  949. return ret;
  950. return i915_gem_object_wait_rendering__tail(obj, ring);
  951. }
  952. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  953. * as the object state may change during this call.
  954. */
  955. static __must_check int
  956. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  957. bool readonly)
  958. {
  959. struct drm_device *dev = obj->base.dev;
  960. struct drm_i915_private *dev_priv = dev->dev_private;
  961. struct intel_ring_buffer *ring = obj->ring;
  962. unsigned reset_counter;
  963. u32 seqno;
  964. int ret;
  965. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  966. BUG_ON(!dev_priv->mm.interruptible);
  967. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  968. if (seqno == 0)
  969. return 0;
  970. ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  971. if (ret)
  972. return ret;
  973. ret = i915_gem_check_olr(ring, seqno);
  974. if (ret)
  975. return ret;
  976. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  977. mutex_unlock(&dev->struct_mutex);
  978. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
  979. mutex_lock(&dev->struct_mutex);
  980. if (ret)
  981. return ret;
  982. return i915_gem_object_wait_rendering__tail(obj, ring);
  983. }
  984. /**
  985. * Called when user space prepares to use an object with the CPU, either
  986. * through the mmap ioctl's mapping or a GTT mapping.
  987. */
  988. int
  989. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  990. struct drm_file *file)
  991. {
  992. struct drm_i915_gem_set_domain *args = data;
  993. struct drm_i915_gem_object *obj;
  994. uint32_t read_domains = args->read_domains;
  995. uint32_t write_domain = args->write_domain;
  996. int ret;
  997. /* Only handle setting domains to types used by the CPU. */
  998. if (write_domain & I915_GEM_GPU_DOMAINS)
  999. return -EINVAL;
  1000. if (read_domains & I915_GEM_GPU_DOMAINS)
  1001. return -EINVAL;
  1002. /* Having something in the write domain implies it's in the read
  1003. * domain, and only that read domain. Enforce that in the request.
  1004. */
  1005. if (write_domain != 0 && read_domains != write_domain)
  1006. return -EINVAL;
  1007. ret = i915_mutex_lock_interruptible(dev);
  1008. if (ret)
  1009. return ret;
  1010. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1011. if (&obj->base == NULL) {
  1012. ret = -ENOENT;
  1013. goto unlock;
  1014. }
  1015. /* Try to flush the object off the GPU without holding the lock.
  1016. * We will repeat the flush holding the lock in the normal manner
  1017. * to catch cases where we are gazumped.
  1018. */
  1019. ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
  1020. if (ret)
  1021. goto unref;
  1022. if (read_domains & I915_GEM_DOMAIN_GTT) {
  1023. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1024. /* Silently promote "you're not bound, there was nothing to do"
  1025. * to success, since the client was just asking us to
  1026. * make sure everything was done.
  1027. */
  1028. if (ret == -EINVAL)
  1029. ret = 0;
  1030. } else {
  1031. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1032. }
  1033. unref:
  1034. drm_gem_object_unreference(&obj->base);
  1035. unlock:
  1036. mutex_unlock(&dev->struct_mutex);
  1037. return ret;
  1038. }
  1039. /**
  1040. * Called when user space has done writes to this buffer
  1041. */
  1042. int
  1043. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1044. struct drm_file *file)
  1045. {
  1046. struct drm_i915_gem_sw_finish *args = data;
  1047. struct drm_i915_gem_object *obj;
  1048. int ret = 0;
  1049. ret = i915_mutex_lock_interruptible(dev);
  1050. if (ret)
  1051. return ret;
  1052. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1053. if (&obj->base == NULL) {
  1054. ret = -ENOENT;
  1055. goto unlock;
  1056. }
  1057. /* Pinned buffers may be scanout, so flush the cache */
  1058. if (obj->pin_count)
  1059. i915_gem_object_flush_cpu_write_domain(obj);
  1060. drm_gem_object_unreference(&obj->base);
  1061. unlock:
  1062. mutex_unlock(&dev->struct_mutex);
  1063. return ret;
  1064. }
  1065. /**
  1066. * Maps the contents of an object, returning the address it is mapped
  1067. * into.
  1068. *
  1069. * While the mapping holds a reference on the contents of the object, it doesn't
  1070. * imply a ref on the object itself.
  1071. */
  1072. int
  1073. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1074. struct drm_file *file)
  1075. {
  1076. struct drm_i915_gem_mmap *args = data;
  1077. struct drm_gem_object *obj;
  1078. unsigned long addr;
  1079. obj = drm_gem_object_lookup(dev, file, args->handle);
  1080. if (obj == NULL)
  1081. return -ENOENT;
  1082. /* prime objects have no backing filp to GEM mmap
  1083. * pages from.
  1084. */
  1085. if (!obj->filp) {
  1086. drm_gem_object_unreference_unlocked(obj);
  1087. return -EINVAL;
  1088. }
  1089. addr = vm_mmap(obj->filp, 0, args->size,
  1090. PROT_READ | PROT_WRITE, MAP_SHARED,
  1091. args->offset);
  1092. drm_gem_object_unreference_unlocked(obj);
  1093. if (IS_ERR((void *)addr))
  1094. return addr;
  1095. args->addr_ptr = (uint64_t) addr;
  1096. return 0;
  1097. }
  1098. /**
  1099. * i915_gem_fault - fault a page into the GTT
  1100. * vma: VMA in question
  1101. * vmf: fault info
  1102. *
  1103. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1104. * from userspace. The fault handler takes care of binding the object to
  1105. * the GTT (if needed), allocating and programming a fence register (again,
  1106. * only if needed based on whether the old reg is still valid or the object
  1107. * is tiled) and inserting a new PTE into the faulting process.
  1108. *
  1109. * Note that the faulting process may involve evicting existing objects
  1110. * from the GTT and/or fence registers to make room. So performance may
  1111. * suffer if the GTT working set is large or there are few fence registers
  1112. * left.
  1113. */
  1114. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1115. {
  1116. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1117. struct drm_device *dev = obj->base.dev;
  1118. drm_i915_private_t *dev_priv = dev->dev_private;
  1119. pgoff_t page_offset;
  1120. unsigned long pfn;
  1121. int ret = 0;
  1122. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1123. /* We don't use vmf->pgoff since that has the fake offset */
  1124. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1125. PAGE_SHIFT;
  1126. ret = i915_mutex_lock_interruptible(dev);
  1127. if (ret)
  1128. goto out;
  1129. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1130. /* Access to snoopable pages through the GTT is incoherent. */
  1131. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1132. ret = -EINVAL;
  1133. goto unlock;
  1134. }
  1135. /* Now bind it into the GTT if needed */
  1136. ret = i915_gem_obj_ggtt_pin(obj, 0, true, false);
  1137. if (ret)
  1138. goto unlock;
  1139. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1140. if (ret)
  1141. goto unpin;
  1142. ret = i915_gem_object_get_fence(obj);
  1143. if (ret)
  1144. goto unpin;
  1145. obj->fault_mappable = true;
  1146. pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
  1147. pfn >>= PAGE_SHIFT;
  1148. pfn += page_offset;
  1149. /* Finally, remap it using the new GTT offset */
  1150. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  1151. unpin:
  1152. i915_gem_object_unpin(obj);
  1153. unlock:
  1154. mutex_unlock(&dev->struct_mutex);
  1155. out:
  1156. switch (ret) {
  1157. case -EIO:
  1158. /* If this -EIO is due to a gpu hang, give the reset code a
  1159. * chance to clean up the mess. Otherwise return the proper
  1160. * SIGBUS. */
  1161. if (i915_terminally_wedged(&dev_priv->gpu_error))
  1162. return VM_FAULT_SIGBUS;
  1163. case -EAGAIN:
  1164. /* Give the error handler a chance to run and move the
  1165. * objects off the GPU active list. Next time we service the
  1166. * fault, we should be able to transition the page into the
  1167. * GTT without touching the GPU (and so avoid further
  1168. * EIO/EGAIN). If the GPU is wedged, then there is no issue
  1169. * with coherency, just lost writes.
  1170. */
  1171. set_need_resched();
  1172. case 0:
  1173. case -ERESTARTSYS:
  1174. case -EINTR:
  1175. case -EBUSY:
  1176. /*
  1177. * EBUSY is ok: this just means that another thread
  1178. * already did the job.
  1179. */
  1180. return VM_FAULT_NOPAGE;
  1181. case -ENOMEM:
  1182. return VM_FAULT_OOM;
  1183. case -ENOSPC:
  1184. return VM_FAULT_SIGBUS;
  1185. default:
  1186. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1187. return VM_FAULT_SIGBUS;
  1188. }
  1189. }
  1190. /**
  1191. * i915_gem_release_mmap - remove physical page mappings
  1192. * @obj: obj in question
  1193. *
  1194. * Preserve the reservation of the mmapping with the DRM core code, but
  1195. * relinquish ownership of the pages back to the system.
  1196. *
  1197. * It is vital that we remove the page mapping if we have mapped a tiled
  1198. * object through the GTT and then lose the fence register due to
  1199. * resource pressure. Similarly if the object has been moved out of the
  1200. * aperture, than pages mapped into userspace must be revoked. Removing the
  1201. * mapping will then trigger a page fault on the next user access, allowing
  1202. * fixup by i915_gem_fault().
  1203. */
  1204. void
  1205. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1206. {
  1207. if (!obj->fault_mappable)
  1208. return;
  1209. if (obj->base.dev->dev_mapping)
  1210. unmap_mapping_range(obj->base.dev->dev_mapping,
  1211. (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
  1212. obj->base.size, 1);
  1213. obj->fault_mappable = false;
  1214. }
  1215. uint32_t
  1216. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1217. {
  1218. uint32_t gtt_size;
  1219. if (INTEL_INFO(dev)->gen >= 4 ||
  1220. tiling_mode == I915_TILING_NONE)
  1221. return size;
  1222. /* Previous chips need a power-of-two fence region when tiling */
  1223. if (INTEL_INFO(dev)->gen == 3)
  1224. gtt_size = 1024*1024;
  1225. else
  1226. gtt_size = 512*1024;
  1227. while (gtt_size < size)
  1228. gtt_size <<= 1;
  1229. return gtt_size;
  1230. }
  1231. /**
  1232. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1233. * @obj: object to check
  1234. *
  1235. * Return the required GTT alignment for an object, taking into account
  1236. * potential fence register mapping.
  1237. */
  1238. uint32_t
  1239. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1240. int tiling_mode, bool fenced)
  1241. {
  1242. /*
  1243. * Minimum alignment is 4k (GTT page size), but might be greater
  1244. * if a fence register is needed for the object.
  1245. */
  1246. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1247. tiling_mode == I915_TILING_NONE)
  1248. return 4096;
  1249. /*
  1250. * Previous chips need to be aligned to the size of the smallest
  1251. * fence register that can contain the object.
  1252. */
  1253. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1254. }
  1255. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1256. {
  1257. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1258. int ret;
  1259. if (obj->base.map_list.map)
  1260. return 0;
  1261. dev_priv->mm.shrinker_no_lock_stealing = true;
  1262. ret = drm_gem_create_mmap_offset(&obj->base);
  1263. if (ret != -ENOSPC)
  1264. goto out;
  1265. /* Badly fragmented mmap space? The only way we can recover
  1266. * space is by destroying unwanted objects. We can't randomly release
  1267. * mmap_offsets as userspace expects them to be persistent for the
  1268. * lifetime of the objects. The closest we can is to release the
  1269. * offsets on purgeable objects by truncating it and marking it purged,
  1270. * which prevents userspace from ever using that object again.
  1271. */
  1272. i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
  1273. ret = drm_gem_create_mmap_offset(&obj->base);
  1274. if (ret != -ENOSPC)
  1275. goto out;
  1276. i915_gem_shrink_all(dev_priv);
  1277. ret = drm_gem_create_mmap_offset(&obj->base);
  1278. out:
  1279. dev_priv->mm.shrinker_no_lock_stealing = false;
  1280. return ret;
  1281. }
  1282. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1283. {
  1284. if (!obj->base.map_list.map)
  1285. return;
  1286. drm_gem_free_mmap_offset(&obj->base);
  1287. }
  1288. int
  1289. i915_gem_mmap_gtt(struct drm_file *file,
  1290. struct drm_device *dev,
  1291. uint32_t handle,
  1292. uint64_t *offset)
  1293. {
  1294. struct drm_i915_private *dev_priv = dev->dev_private;
  1295. struct drm_i915_gem_object *obj;
  1296. int ret;
  1297. ret = i915_mutex_lock_interruptible(dev);
  1298. if (ret)
  1299. return ret;
  1300. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1301. if (&obj->base == NULL) {
  1302. ret = -ENOENT;
  1303. goto unlock;
  1304. }
  1305. if (obj->base.size > dev_priv->gtt.mappable_end) {
  1306. ret = -E2BIG;
  1307. goto out;
  1308. }
  1309. if (obj->madv != I915_MADV_WILLNEED) {
  1310. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1311. ret = -EINVAL;
  1312. goto out;
  1313. }
  1314. ret = i915_gem_object_create_mmap_offset(obj);
  1315. if (ret)
  1316. goto out;
  1317. *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
  1318. out:
  1319. drm_gem_object_unreference(&obj->base);
  1320. unlock:
  1321. mutex_unlock(&dev->struct_mutex);
  1322. return ret;
  1323. }
  1324. /**
  1325. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1326. * @dev: DRM device
  1327. * @data: GTT mapping ioctl data
  1328. * @file: GEM object info
  1329. *
  1330. * Simply returns the fake offset to userspace so it can mmap it.
  1331. * The mmap call will end up in drm_gem_mmap(), which will set things
  1332. * up so we can get faults in the handler above.
  1333. *
  1334. * The fault handler will take care of binding the object into the GTT
  1335. * (since it may have been evicted to make room for something), allocating
  1336. * a fence register, and mapping the appropriate aperture address into
  1337. * userspace.
  1338. */
  1339. int
  1340. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1341. struct drm_file *file)
  1342. {
  1343. struct drm_i915_gem_mmap_gtt *args = data;
  1344. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1345. }
  1346. /* Immediately discard the backing storage */
  1347. static void
  1348. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1349. {
  1350. struct inode *inode;
  1351. i915_gem_object_free_mmap_offset(obj);
  1352. if (obj->base.filp == NULL)
  1353. return;
  1354. /* Our goal here is to return as much of the memory as
  1355. * is possible back to the system as we are called from OOM.
  1356. * To do this we must instruct the shmfs to drop all of its
  1357. * backing pages, *now*.
  1358. */
  1359. inode = file_inode(obj->base.filp);
  1360. shmem_truncate_range(inode, 0, (loff_t)-1);
  1361. obj->madv = __I915_MADV_PURGED;
  1362. }
  1363. static inline int
  1364. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1365. {
  1366. return obj->madv == I915_MADV_DONTNEED;
  1367. }
  1368. static void
  1369. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1370. {
  1371. struct sg_page_iter sg_iter;
  1372. int ret;
  1373. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1374. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1375. if (ret) {
  1376. /* In the event of a disaster, abandon all caches and
  1377. * hope for the best.
  1378. */
  1379. WARN_ON(ret != -EIO);
  1380. i915_gem_clflush_object(obj);
  1381. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1382. }
  1383. if (i915_gem_object_needs_bit17_swizzle(obj))
  1384. i915_gem_object_save_bit_17_swizzle(obj);
  1385. if (obj->madv == I915_MADV_DONTNEED)
  1386. obj->dirty = 0;
  1387. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1388. struct page *page = sg_page_iter_page(&sg_iter);
  1389. if (obj->dirty)
  1390. set_page_dirty(page);
  1391. if (obj->madv == I915_MADV_WILLNEED)
  1392. mark_page_accessed(page);
  1393. page_cache_release(page);
  1394. }
  1395. obj->dirty = 0;
  1396. sg_free_table(obj->pages);
  1397. kfree(obj->pages);
  1398. }
  1399. int
  1400. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1401. {
  1402. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1403. if (obj->pages == NULL)
  1404. return 0;
  1405. if (obj->pages_pin_count)
  1406. return -EBUSY;
  1407. BUG_ON(i915_gem_obj_bound_any(obj));
  1408. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1409. * array, hence protect them from being reaped by removing them from gtt
  1410. * lists early. */
  1411. list_del(&obj->global_list);
  1412. ops->put_pages(obj);
  1413. obj->pages = NULL;
  1414. if (i915_gem_object_is_purgeable(obj))
  1415. i915_gem_object_truncate(obj);
  1416. return 0;
  1417. }
  1418. static long
  1419. __i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
  1420. bool purgeable_only)
  1421. {
  1422. struct drm_i915_gem_object *obj, *next;
  1423. long count = 0;
  1424. list_for_each_entry_safe(obj, next,
  1425. &dev_priv->mm.unbound_list,
  1426. global_list) {
  1427. if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
  1428. i915_gem_object_put_pages(obj) == 0) {
  1429. count += obj->base.size >> PAGE_SHIFT;
  1430. if (count >= target)
  1431. return count;
  1432. }
  1433. }
  1434. list_for_each_entry_safe(obj, next, &dev_priv->mm.bound_list,
  1435. global_list) {
  1436. struct i915_vma *vma, *v;
  1437. if (!i915_gem_object_is_purgeable(obj) && purgeable_only)
  1438. continue;
  1439. list_for_each_entry_safe(vma, v, &obj->vma_list, vma_link)
  1440. if (i915_vma_unbind(vma))
  1441. break;
  1442. if (!i915_gem_object_put_pages(obj)) {
  1443. count += obj->base.size >> PAGE_SHIFT;
  1444. if (count >= target)
  1445. return count;
  1446. }
  1447. }
  1448. return count;
  1449. }
  1450. static long
  1451. i915_gem_purge(struct drm_i915_private *dev_priv, long target)
  1452. {
  1453. return __i915_gem_shrink(dev_priv, target, true);
  1454. }
  1455. static void
  1456. i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  1457. {
  1458. struct drm_i915_gem_object *obj, *next;
  1459. i915_gem_evict_everything(dev_priv->dev);
  1460. list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list,
  1461. global_list)
  1462. i915_gem_object_put_pages(obj);
  1463. }
  1464. static int
  1465. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1466. {
  1467. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1468. int page_count, i;
  1469. struct address_space *mapping;
  1470. struct sg_table *st;
  1471. struct scatterlist *sg;
  1472. struct sg_page_iter sg_iter;
  1473. struct page *page;
  1474. unsigned long last_pfn = 0; /* suppress gcc warning */
  1475. gfp_t gfp;
  1476. /* Assert that the object is not currently in any GPU domain. As it
  1477. * wasn't in the GTT, there shouldn't be any way it could have been in
  1478. * a GPU cache
  1479. */
  1480. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1481. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1482. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1483. if (st == NULL)
  1484. return -ENOMEM;
  1485. page_count = obj->base.size / PAGE_SIZE;
  1486. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1487. sg_free_table(st);
  1488. kfree(st);
  1489. return -ENOMEM;
  1490. }
  1491. /* Get the list of pages out of our struct file. They'll be pinned
  1492. * at this point until we release them.
  1493. *
  1494. * Fail silently without starting the shrinker
  1495. */
  1496. mapping = file_inode(obj->base.filp)->i_mapping;
  1497. gfp = mapping_gfp_mask(mapping);
  1498. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1499. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1500. sg = st->sgl;
  1501. st->nents = 0;
  1502. for (i = 0; i < page_count; i++) {
  1503. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1504. if (IS_ERR(page)) {
  1505. i915_gem_purge(dev_priv, page_count);
  1506. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1507. }
  1508. if (IS_ERR(page)) {
  1509. /* We've tried hard to allocate the memory by reaping
  1510. * our own buffer, now let the real VM do its job and
  1511. * go down in flames if truly OOM.
  1512. */
  1513. gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
  1514. gfp |= __GFP_IO | __GFP_WAIT;
  1515. i915_gem_shrink_all(dev_priv);
  1516. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1517. if (IS_ERR(page))
  1518. goto err_pages;
  1519. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1520. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1521. }
  1522. #ifdef CONFIG_SWIOTLB
  1523. if (swiotlb_nr_tbl()) {
  1524. st->nents++;
  1525. sg_set_page(sg, page, PAGE_SIZE, 0);
  1526. sg = sg_next(sg);
  1527. continue;
  1528. }
  1529. #endif
  1530. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1531. if (i)
  1532. sg = sg_next(sg);
  1533. st->nents++;
  1534. sg_set_page(sg, page, PAGE_SIZE, 0);
  1535. } else {
  1536. sg->length += PAGE_SIZE;
  1537. }
  1538. last_pfn = page_to_pfn(page);
  1539. }
  1540. #ifdef CONFIG_SWIOTLB
  1541. if (!swiotlb_nr_tbl())
  1542. #endif
  1543. sg_mark_end(sg);
  1544. obj->pages = st;
  1545. if (i915_gem_object_needs_bit17_swizzle(obj))
  1546. i915_gem_object_do_bit_17_swizzle(obj);
  1547. return 0;
  1548. err_pages:
  1549. sg_mark_end(sg);
  1550. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1551. page_cache_release(sg_page_iter_page(&sg_iter));
  1552. sg_free_table(st);
  1553. kfree(st);
  1554. return PTR_ERR(page);
  1555. }
  1556. /* Ensure that the associated pages are gathered from the backing storage
  1557. * and pinned into our object. i915_gem_object_get_pages() may be called
  1558. * multiple times before they are released by a single call to
  1559. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1560. * either as a result of memory pressure (reaping pages under the shrinker)
  1561. * or as the object is itself released.
  1562. */
  1563. int
  1564. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1565. {
  1566. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1567. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1568. int ret;
  1569. if (obj->pages)
  1570. return 0;
  1571. if (obj->madv != I915_MADV_WILLNEED) {
  1572. DRM_ERROR("Attempting to obtain a purgeable object\n");
  1573. return -EINVAL;
  1574. }
  1575. BUG_ON(obj->pages_pin_count);
  1576. ret = ops->get_pages(obj);
  1577. if (ret)
  1578. return ret;
  1579. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1580. return 0;
  1581. }
  1582. void
  1583. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1584. struct intel_ring_buffer *ring)
  1585. {
  1586. struct drm_device *dev = obj->base.dev;
  1587. struct drm_i915_private *dev_priv = dev->dev_private;
  1588. struct i915_address_space *vm = &dev_priv->gtt.base;
  1589. u32 seqno = intel_ring_get_seqno(ring);
  1590. BUG_ON(ring == NULL);
  1591. if (obj->ring != ring && obj->last_write_seqno) {
  1592. /* Keep the seqno relative to the current ring */
  1593. obj->last_write_seqno = seqno;
  1594. }
  1595. obj->ring = ring;
  1596. /* Add a reference if we're newly entering the active list. */
  1597. if (!obj->active) {
  1598. drm_gem_object_reference(&obj->base);
  1599. obj->active = 1;
  1600. }
  1601. /* Move from whatever list we were on to the tail of execution. */
  1602. list_move_tail(&obj->mm_list, &vm->active_list);
  1603. list_move_tail(&obj->ring_list, &ring->active_list);
  1604. obj->last_read_seqno = seqno;
  1605. if (obj->fenced_gpu_access) {
  1606. obj->last_fenced_seqno = seqno;
  1607. /* Bump MRU to take account of the delayed flush */
  1608. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  1609. struct drm_i915_fence_reg *reg;
  1610. reg = &dev_priv->fence_regs[obj->fence_reg];
  1611. list_move_tail(&reg->lru_list,
  1612. &dev_priv->mm.fence_list);
  1613. }
  1614. }
  1615. }
  1616. static void
  1617. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1618. {
  1619. struct drm_device *dev = obj->base.dev;
  1620. struct drm_i915_private *dev_priv = dev->dev_private;
  1621. struct i915_address_space *vm = &dev_priv->gtt.base;
  1622. BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
  1623. BUG_ON(!obj->active);
  1624. list_move_tail(&obj->mm_list, &vm->inactive_list);
  1625. list_del_init(&obj->ring_list);
  1626. obj->ring = NULL;
  1627. obj->last_read_seqno = 0;
  1628. obj->last_write_seqno = 0;
  1629. obj->base.write_domain = 0;
  1630. obj->last_fenced_seqno = 0;
  1631. obj->fenced_gpu_access = false;
  1632. obj->active = 0;
  1633. drm_gem_object_unreference(&obj->base);
  1634. WARN_ON(i915_verify_lists(dev));
  1635. }
  1636. static int
  1637. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  1638. {
  1639. struct drm_i915_private *dev_priv = dev->dev_private;
  1640. struct intel_ring_buffer *ring;
  1641. int ret, i, j;
  1642. /* Carefully retire all requests without writing to the rings */
  1643. for_each_ring(ring, dev_priv, i) {
  1644. ret = intel_ring_idle(ring);
  1645. if (ret)
  1646. return ret;
  1647. }
  1648. i915_gem_retire_requests(dev);
  1649. /* Finally reset hw state */
  1650. for_each_ring(ring, dev_priv, i) {
  1651. intel_ring_init_seqno(ring, seqno);
  1652. for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
  1653. ring->sync_seqno[j] = 0;
  1654. }
  1655. return 0;
  1656. }
  1657. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  1658. {
  1659. struct drm_i915_private *dev_priv = dev->dev_private;
  1660. int ret;
  1661. if (seqno == 0)
  1662. return -EINVAL;
  1663. /* HWS page needs to be set less than what we
  1664. * will inject to ring
  1665. */
  1666. ret = i915_gem_init_seqno(dev, seqno - 1);
  1667. if (ret)
  1668. return ret;
  1669. /* Carefully set the last_seqno value so that wrap
  1670. * detection still works
  1671. */
  1672. dev_priv->next_seqno = seqno;
  1673. dev_priv->last_seqno = seqno - 1;
  1674. if (dev_priv->last_seqno == 0)
  1675. dev_priv->last_seqno--;
  1676. return 0;
  1677. }
  1678. int
  1679. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  1680. {
  1681. struct drm_i915_private *dev_priv = dev->dev_private;
  1682. /* reserve 0 for non-seqno */
  1683. if (dev_priv->next_seqno == 0) {
  1684. int ret = i915_gem_init_seqno(dev, 0);
  1685. if (ret)
  1686. return ret;
  1687. dev_priv->next_seqno = 1;
  1688. }
  1689. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  1690. return 0;
  1691. }
  1692. int __i915_add_request(struct intel_ring_buffer *ring,
  1693. struct drm_file *file,
  1694. struct drm_i915_gem_object *obj,
  1695. u32 *out_seqno)
  1696. {
  1697. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1698. struct drm_i915_gem_request *request;
  1699. u32 request_ring_position, request_start;
  1700. int was_empty;
  1701. int ret;
  1702. request_start = intel_ring_get_tail(ring);
  1703. /*
  1704. * Emit any outstanding flushes - execbuf can fail to emit the flush
  1705. * after having emitted the batchbuffer command. Hence we need to fix
  1706. * things up similar to emitting the lazy request. The difference here
  1707. * is that the flush _must_ happen before the next request, no matter
  1708. * what.
  1709. */
  1710. ret = intel_ring_flush_all_caches(ring);
  1711. if (ret)
  1712. return ret;
  1713. request = kmalloc(sizeof(*request), GFP_KERNEL);
  1714. if (request == NULL)
  1715. return -ENOMEM;
  1716. /* Record the position of the start of the request so that
  1717. * should we detect the updated seqno part-way through the
  1718. * GPU processing the request, we never over-estimate the
  1719. * position of the head.
  1720. */
  1721. request_ring_position = intel_ring_get_tail(ring);
  1722. ret = ring->add_request(ring);
  1723. if (ret) {
  1724. kfree(request);
  1725. return ret;
  1726. }
  1727. request->seqno = intel_ring_get_seqno(ring);
  1728. request->ring = ring;
  1729. request->head = request_start;
  1730. request->tail = request_ring_position;
  1731. request->ctx = ring->last_context;
  1732. request->batch_obj = obj;
  1733. /* Whilst this request exists, batch_obj will be on the
  1734. * active_list, and so will hold the active reference. Only when this
  1735. * request is retired will the the batch_obj be moved onto the
  1736. * inactive_list and lose its active reference. Hence we do not need
  1737. * to explicitly hold another reference here.
  1738. */
  1739. if (request->ctx)
  1740. i915_gem_context_reference(request->ctx);
  1741. request->emitted_jiffies = jiffies;
  1742. was_empty = list_empty(&ring->request_list);
  1743. list_add_tail(&request->list, &ring->request_list);
  1744. request->file_priv = NULL;
  1745. if (file) {
  1746. struct drm_i915_file_private *file_priv = file->driver_priv;
  1747. spin_lock(&file_priv->mm.lock);
  1748. request->file_priv = file_priv;
  1749. list_add_tail(&request->client_list,
  1750. &file_priv->mm.request_list);
  1751. spin_unlock(&file_priv->mm.lock);
  1752. }
  1753. trace_i915_gem_request_add(ring, request->seqno);
  1754. ring->outstanding_lazy_request = 0;
  1755. if (!dev_priv->ums.mm_suspended) {
  1756. i915_queue_hangcheck(ring->dev);
  1757. if (was_empty) {
  1758. queue_delayed_work(dev_priv->wq,
  1759. &dev_priv->mm.retire_work,
  1760. round_jiffies_up_relative(HZ));
  1761. intel_mark_busy(dev_priv->dev);
  1762. }
  1763. }
  1764. if (out_seqno)
  1765. *out_seqno = request->seqno;
  1766. return 0;
  1767. }
  1768. static inline void
  1769. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1770. {
  1771. struct drm_i915_file_private *file_priv = request->file_priv;
  1772. if (!file_priv)
  1773. return;
  1774. spin_lock(&file_priv->mm.lock);
  1775. if (request->file_priv) {
  1776. list_del(&request->client_list);
  1777. request->file_priv = NULL;
  1778. }
  1779. spin_unlock(&file_priv->mm.lock);
  1780. }
  1781. static bool i915_head_inside_object(u32 acthd, struct drm_i915_gem_object *obj,
  1782. struct i915_address_space *vm)
  1783. {
  1784. if (acthd >= i915_gem_obj_offset(obj, vm) &&
  1785. acthd < i915_gem_obj_offset(obj, vm) + obj->base.size)
  1786. return true;
  1787. return false;
  1788. }
  1789. static bool i915_head_inside_request(const u32 acthd_unmasked,
  1790. const u32 request_start,
  1791. const u32 request_end)
  1792. {
  1793. const u32 acthd = acthd_unmasked & HEAD_ADDR;
  1794. if (request_start < request_end) {
  1795. if (acthd >= request_start && acthd < request_end)
  1796. return true;
  1797. } else if (request_start > request_end) {
  1798. if (acthd >= request_start || acthd < request_end)
  1799. return true;
  1800. }
  1801. return false;
  1802. }
  1803. static struct i915_address_space *
  1804. request_to_vm(struct drm_i915_gem_request *request)
  1805. {
  1806. struct drm_i915_private *dev_priv = request->ring->dev->dev_private;
  1807. struct i915_address_space *vm;
  1808. vm = &dev_priv->gtt.base;
  1809. return vm;
  1810. }
  1811. static bool i915_request_guilty(struct drm_i915_gem_request *request,
  1812. const u32 acthd, bool *inside)
  1813. {
  1814. /* There is a possibility that unmasked head address
  1815. * pointing inside the ring, matches the batch_obj address range.
  1816. * However this is extremely unlikely.
  1817. */
  1818. if (request->batch_obj) {
  1819. if (i915_head_inside_object(acthd, request->batch_obj,
  1820. request_to_vm(request))) {
  1821. *inside = true;
  1822. return true;
  1823. }
  1824. }
  1825. if (i915_head_inside_request(acthd, request->head, request->tail)) {
  1826. *inside = false;
  1827. return true;
  1828. }
  1829. return false;
  1830. }
  1831. static void i915_set_reset_status(struct intel_ring_buffer *ring,
  1832. struct drm_i915_gem_request *request,
  1833. u32 acthd)
  1834. {
  1835. struct i915_ctx_hang_stats *hs = NULL;
  1836. bool inside, guilty;
  1837. unsigned long offset = 0;
  1838. /* Innocent until proven guilty */
  1839. guilty = false;
  1840. if (request->batch_obj)
  1841. offset = i915_gem_obj_offset(request->batch_obj,
  1842. request_to_vm(request));
  1843. if (ring->hangcheck.action != wait &&
  1844. i915_request_guilty(request, acthd, &inside)) {
  1845. DRM_ERROR("%s hung %s bo (0x%lx ctx %d) at 0x%x\n",
  1846. ring->name,
  1847. inside ? "inside" : "flushing",
  1848. offset,
  1849. request->ctx ? request->ctx->id : 0,
  1850. acthd);
  1851. guilty = true;
  1852. }
  1853. /* If contexts are disabled or this is the default context, use
  1854. * file_priv->reset_state
  1855. */
  1856. if (request->ctx && request->ctx->id != DEFAULT_CONTEXT_ID)
  1857. hs = &request->ctx->hang_stats;
  1858. else if (request->file_priv)
  1859. hs = &request->file_priv->hang_stats;
  1860. if (hs) {
  1861. if (guilty)
  1862. hs->batch_active++;
  1863. else
  1864. hs->batch_pending++;
  1865. }
  1866. }
  1867. static void i915_gem_free_request(struct drm_i915_gem_request *request)
  1868. {
  1869. list_del(&request->list);
  1870. i915_gem_request_remove_from_client(request);
  1871. if (request->ctx)
  1872. i915_gem_context_unreference(request->ctx);
  1873. kfree(request);
  1874. }
  1875. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1876. struct intel_ring_buffer *ring)
  1877. {
  1878. u32 completed_seqno;
  1879. u32 acthd;
  1880. acthd = intel_ring_get_active_head(ring);
  1881. completed_seqno = ring->get_seqno(ring, false);
  1882. while (!list_empty(&ring->request_list)) {
  1883. struct drm_i915_gem_request *request;
  1884. request = list_first_entry(&ring->request_list,
  1885. struct drm_i915_gem_request,
  1886. list);
  1887. if (request->seqno > completed_seqno)
  1888. i915_set_reset_status(ring, request, acthd);
  1889. i915_gem_free_request(request);
  1890. }
  1891. while (!list_empty(&ring->active_list)) {
  1892. struct drm_i915_gem_object *obj;
  1893. obj = list_first_entry(&ring->active_list,
  1894. struct drm_i915_gem_object,
  1895. ring_list);
  1896. i915_gem_object_move_to_inactive(obj);
  1897. }
  1898. }
  1899. void i915_gem_restore_fences(struct drm_device *dev)
  1900. {
  1901. struct drm_i915_private *dev_priv = dev->dev_private;
  1902. int i;
  1903. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1904. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1905. /*
  1906. * Commit delayed tiling changes if we have an object still
  1907. * attached to the fence, otherwise just clear the fence.
  1908. */
  1909. if (reg->obj) {
  1910. i915_gem_object_update_fence(reg->obj, reg,
  1911. reg->obj->tiling_mode);
  1912. } else {
  1913. i915_gem_write_fence(dev, i, NULL);
  1914. }
  1915. }
  1916. }
  1917. void i915_gem_reset(struct drm_device *dev)
  1918. {
  1919. struct drm_i915_private *dev_priv = dev->dev_private;
  1920. struct intel_ring_buffer *ring;
  1921. int i;
  1922. for_each_ring(ring, dev_priv, i)
  1923. i915_gem_reset_ring_lists(dev_priv, ring);
  1924. i915_gem_restore_fences(dev);
  1925. }
  1926. /**
  1927. * This function clears the request list as sequence numbers are passed.
  1928. */
  1929. void
  1930. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1931. {
  1932. uint32_t seqno;
  1933. if (list_empty(&ring->request_list))
  1934. return;
  1935. WARN_ON(i915_verify_lists(ring->dev));
  1936. seqno = ring->get_seqno(ring, true);
  1937. while (!list_empty(&ring->request_list)) {
  1938. struct drm_i915_gem_request *request;
  1939. request = list_first_entry(&ring->request_list,
  1940. struct drm_i915_gem_request,
  1941. list);
  1942. if (!i915_seqno_passed(seqno, request->seqno))
  1943. break;
  1944. trace_i915_gem_request_retire(ring, request->seqno);
  1945. /* We know the GPU must have read the request to have
  1946. * sent us the seqno + interrupt, so use the position
  1947. * of tail of the request to update the last known position
  1948. * of the GPU head.
  1949. */
  1950. ring->last_retired_head = request->tail;
  1951. i915_gem_free_request(request);
  1952. }
  1953. /* Move any buffers on the active list that are no longer referenced
  1954. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1955. */
  1956. while (!list_empty(&ring->active_list)) {
  1957. struct drm_i915_gem_object *obj;
  1958. obj = list_first_entry(&ring->active_list,
  1959. struct drm_i915_gem_object,
  1960. ring_list);
  1961. if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  1962. break;
  1963. i915_gem_object_move_to_inactive(obj);
  1964. }
  1965. if (unlikely(ring->trace_irq_seqno &&
  1966. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  1967. ring->irq_put(ring);
  1968. ring->trace_irq_seqno = 0;
  1969. }
  1970. WARN_ON(i915_verify_lists(ring->dev));
  1971. }
  1972. void
  1973. i915_gem_retire_requests(struct drm_device *dev)
  1974. {
  1975. drm_i915_private_t *dev_priv = dev->dev_private;
  1976. struct intel_ring_buffer *ring;
  1977. int i;
  1978. for_each_ring(ring, dev_priv, i)
  1979. i915_gem_retire_requests_ring(ring);
  1980. }
  1981. static void
  1982. i915_gem_retire_work_handler(struct work_struct *work)
  1983. {
  1984. drm_i915_private_t *dev_priv;
  1985. struct drm_device *dev;
  1986. struct intel_ring_buffer *ring;
  1987. bool idle;
  1988. int i;
  1989. dev_priv = container_of(work, drm_i915_private_t,
  1990. mm.retire_work.work);
  1991. dev = dev_priv->dev;
  1992. /* Come back later if the device is busy... */
  1993. if (!mutex_trylock(&dev->struct_mutex)) {
  1994. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1995. round_jiffies_up_relative(HZ));
  1996. return;
  1997. }
  1998. i915_gem_retire_requests(dev);
  1999. /* Send a periodic flush down the ring so we don't hold onto GEM
  2000. * objects indefinitely.
  2001. */
  2002. idle = true;
  2003. for_each_ring(ring, dev_priv, i) {
  2004. if (ring->gpu_caches_dirty)
  2005. i915_add_request(ring, NULL);
  2006. idle &= list_empty(&ring->request_list);
  2007. }
  2008. if (!dev_priv->ums.mm_suspended && !idle)
  2009. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2010. round_jiffies_up_relative(HZ));
  2011. if (idle)
  2012. intel_mark_idle(dev);
  2013. mutex_unlock(&dev->struct_mutex);
  2014. }
  2015. /**
  2016. * Ensures that an object will eventually get non-busy by flushing any required
  2017. * write domains, emitting any outstanding lazy request and retiring and
  2018. * completed requests.
  2019. */
  2020. static int
  2021. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2022. {
  2023. int ret;
  2024. if (obj->active) {
  2025. ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  2026. if (ret)
  2027. return ret;
  2028. i915_gem_retire_requests_ring(obj->ring);
  2029. }
  2030. return 0;
  2031. }
  2032. /**
  2033. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2034. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2035. *
  2036. * Returns 0 if successful, else an error is returned with the remaining time in
  2037. * the timeout parameter.
  2038. * -ETIME: object is still busy after timeout
  2039. * -ERESTARTSYS: signal interrupted the wait
  2040. * -ENONENT: object doesn't exist
  2041. * Also possible, but rare:
  2042. * -EAGAIN: GPU wedged
  2043. * -ENOMEM: damn
  2044. * -ENODEV: Internal IRQ fail
  2045. * -E?: The add request failed
  2046. *
  2047. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2048. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2049. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2050. * without holding struct_mutex the object may become re-busied before this
  2051. * function completes. A similar but shorter * race condition exists in the busy
  2052. * ioctl
  2053. */
  2054. int
  2055. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2056. {
  2057. drm_i915_private_t *dev_priv = dev->dev_private;
  2058. struct drm_i915_gem_wait *args = data;
  2059. struct drm_i915_gem_object *obj;
  2060. struct intel_ring_buffer *ring = NULL;
  2061. struct timespec timeout_stack, *timeout = NULL;
  2062. unsigned reset_counter;
  2063. u32 seqno = 0;
  2064. int ret = 0;
  2065. if (args->timeout_ns >= 0) {
  2066. timeout_stack = ns_to_timespec(args->timeout_ns);
  2067. timeout = &timeout_stack;
  2068. }
  2069. ret = i915_mutex_lock_interruptible(dev);
  2070. if (ret)
  2071. return ret;
  2072. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2073. if (&obj->base == NULL) {
  2074. mutex_unlock(&dev->struct_mutex);
  2075. return -ENOENT;
  2076. }
  2077. /* Need to make sure the object gets inactive eventually. */
  2078. ret = i915_gem_object_flush_active(obj);
  2079. if (ret)
  2080. goto out;
  2081. if (obj->active) {
  2082. seqno = obj->last_read_seqno;
  2083. ring = obj->ring;
  2084. }
  2085. if (seqno == 0)
  2086. goto out;
  2087. /* Do this after OLR check to make sure we make forward progress polling
  2088. * on this IOCTL with a 0 timeout (like busy ioctl)
  2089. */
  2090. if (!args->timeout_ns) {
  2091. ret = -ETIME;
  2092. goto out;
  2093. }
  2094. drm_gem_object_unreference(&obj->base);
  2095. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2096. mutex_unlock(&dev->struct_mutex);
  2097. ret = __wait_seqno(ring, seqno, reset_counter, true, timeout);
  2098. if (timeout)
  2099. args->timeout_ns = timespec_to_ns(timeout);
  2100. return ret;
  2101. out:
  2102. drm_gem_object_unreference(&obj->base);
  2103. mutex_unlock(&dev->struct_mutex);
  2104. return ret;
  2105. }
  2106. /**
  2107. * i915_gem_object_sync - sync an object to a ring.
  2108. *
  2109. * @obj: object which may be in use on another ring.
  2110. * @to: ring we wish to use the object on. May be NULL.
  2111. *
  2112. * This code is meant to abstract object synchronization with the GPU.
  2113. * Calling with NULL implies synchronizing the object with the CPU
  2114. * rather than a particular GPU ring.
  2115. *
  2116. * Returns 0 if successful, else propagates up the lower layer error.
  2117. */
  2118. int
  2119. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2120. struct intel_ring_buffer *to)
  2121. {
  2122. struct intel_ring_buffer *from = obj->ring;
  2123. u32 seqno;
  2124. int ret, idx;
  2125. if (from == NULL || to == from)
  2126. return 0;
  2127. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  2128. return i915_gem_object_wait_rendering(obj, false);
  2129. idx = intel_ring_sync_index(from, to);
  2130. seqno = obj->last_read_seqno;
  2131. if (seqno <= from->sync_seqno[idx])
  2132. return 0;
  2133. ret = i915_gem_check_olr(obj->ring, seqno);
  2134. if (ret)
  2135. return ret;
  2136. ret = to->sync_to(to, from, seqno);
  2137. if (!ret)
  2138. /* We use last_read_seqno because sync_to()
  2139. * might have just caused seqno wrap under
  2140. * the radar.
  2141. */
  2142. from->sync_seqno[idx] = obj->last_read_seqno;
  2143. return ret;
  2144. }
  2145. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2146. {
  2147. u32 old_write_domain, old_read_domains;
  2148. /* Force a pagefault for domain tracking on next user access */
  2149. i915_gem_release_mmap(obj);
  2150. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2151. return;
  2152. /* Wait for any direct GTT access to complete */
  2153. mb();
  2154. old_read_domains = obj->base.read_domains;
  2155. old_write_domain = obj->base.write_domain;
  2156. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2157. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2158. trace_i915_gem_object_change_domain(obj,
  2159. old_read_domains,
  2160. old_write_domain);
  2161. }
  2162. int i915_vma_unbind(struct i915_vma *vma)
  2163. {
  2164. struct drm_i915_gem_object *obj = vma->obj;
  2165. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2166. int ret;
  2167. if (list_empty(&vma->vma_link))
  2168. return 0;
  2169. if (obj->pin_count)
  2170. return -EBUSY;
  2171. BUG_ON(obj->pages == NULL);
  2172. ret = i915_gem_object_finish_gpu(obj);
  2173. if (ret)
  2174. return ret;
  2175. /* Continue on if we fail due to EIO, the GPU is hung so we
  2176. * should be safe and we need to cleanup or else we might
  2177. * cause memory corruption through use-after-free.
  2178. */
  2179. i915_gem_object_finish_gtt(obj);
  2180. /* release the fence reg _after_ flushing */
  2181. ret = i915_gem_object_put_fence(obj);
  2182. if (ret)
  2183. return ret;
  2184. trace_i915_vma_unbind(vma);
  2185. if (obj->has_global_gtt_mapping)
  2186. i915_gem_gtt_unbind_object(obj);
  2187. if (obj->has_aliasing_ppgtt_mapping) {
  2188. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  2189. obj->has_aliasing_ppgtt_mapping = 0;
  2190. }
  2191. i915_gem_gtt_finish_object(obj);
  2192. i915_gem_object_unpin_pages(obj);
  2193. list_del(&obj->mm_list);
  2194. /* Avoid an unnecessary call to unbind on rebind. */
  2195. if (i915_is_ggtt(vma->vm))
  2196. obj->map_and_fenceable = true;
  2197. list_del(&vma->vma_link);
  2198. drm_mm_remove_node(&vma->node);
  2199. i915_gem_vma_destroy(vma);
  2200. /* Since the unbound list is global, only move to that list if
  2201. * no more VMAs exist.
  2202. * NB: Until we have real VMAs there will only ever be one */
  2203. WARN_ON(!list_empty(&obj->vma_list));
  2204. if (list_empty(&obj->vma_list))
  2205. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2206. return 0;
  2207. }
  2208. /**
  2209. * Unbinds an object from the global GTT aperture.
  2210. */
  2211. int
  2212. i915_gem_object_ggtt_unbind(struct drm_i915_gem_object *obj)
  2213. {
  2214. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2215. struct i915_address_space *ggtt = &dev_priv->gtt.base;
  2216. if (!i915_gem_obj_ggtt_bound(obj));
  2217. return 0;
  2218. if (obj->pin_count)
  2219. return -EBUSY;
  2220. BUG_ON(obj->pages == NULL);
  2221. return i915_vma_unbind(i915_gem_obj_to_vma(obj, ggtt));
  2222. }
  2223. int i915_gpu_idle(struct drm_device *dev)
  2224. {
  2225. drm_i915_private_t *dev_priv = dev->dev_private;
  2226. struct intel_ring_buffer *ring;
  2227. int ret, i;
  2228. /* Flush everything onto the inactive list. */
  2229. for_each_ring(ring, dev_priv, i) {
  2230. ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
  2231. if (ret)
  2232. return ret;
  2233. ret = intel_ring_idle(ring);
  2234. if (ret)
  2235. return ret;
  2236. }
  2237. return 0;
  2238. }
  2239. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2240. struct drm_i915_gem_object *obj)
  2241. {
  2242. drm_i915_private_t *dev_priv = dev->dev_private;
  2243. int fence_reg;
  2244. int fence_pitch_shift;
  2245. if (INTEL_INFO(dev)->gen >= 6) {
  2246. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2247. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2248. } else {
  2249. fence_reg = FENCE_REG_965_0;
  2250. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2251. }
  2252. fence_reg += reg * 8;
  2253. /* To w/a incoherency with non-atomic 64-bit register updates,
  2254. * we split the 64-bit update into two 32-bit writes. In order
  2255. * for a partial fence not to be evaluated between writes, we
  2256. * precede the update with write to turn off the fence register,
  2257. * and only enable the fence as the last step.
  2258. *
  2259. * For extra levels of paranoia, we make sure each step lands
  2260. * before applying the next step.
  2261. */
  2262. I915_WRITE(fence_reg, 0);
  2263. POSTING_READ(fence_reg);
  2264. if (obj) {
  2265. u32 size = i915_gem_obj_ggtt_size(obj);
  2266. uint64_t val;
  2267. val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
  2268. 0xfffff000) << 32;
  2269. val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
  2270. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2271. if (obj->tiling_mode == I915_TILING_Y)
  2272. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2273. val |= I965_FENCE_REG_VALID;
  2274. I915_WRITE(fence_reg + 4, val >> 32);
  2275. POSTING_READ(fence_reg + 4);
  2276. I915_WRITE(fence_reg + 0, val);
  2277. POSTING_READ(fence_reg);
  2278. } else {
  2279. I915_WRITE(fence_reg + 4, 0);
  2280. POSTING_READ(fence_reg + 4);
  2281. }
  2282. }
  2283. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2284. struct drm_i915_gem_object *obj)
  2285. {
  2286. drm_i915_private_t *dev_priv = dev->dev_private;
  2287. u32 val;
  2288. if (obj) {
  2289. u32 size = i915_gem_obj_ggtt_size(obj);
  2290. int pitch_val;
  2291. int tile_width;
  2292. WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
  2293. (size & -size) != size ||
  2294. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2295. "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2296. i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
  2297. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2298. tile_width = 128;
  2299. else
  2300. tile_width = 512;
  2301. /* Note: pitch better be a power of two tile widths */
  2302. pitch_val = obj->stride / tile_width;
  2303. pitch_val = ffs(pitch_val) - 1;
  2304. val = i915_gem_obj_ggtt_offset(obj);
  2305. if (obj->tiling_mode == I915_TILING_Y)
  2306. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2307. val |= I915_FENCE_SIZE_BITS(size);
  2308. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2309. val |= I830_FENCE_REG_VALID;
  2310. } else
  2311. val = 0;
  2312. if (reg < 8)
  2313. reg = FENCE_REG_830_0 + reg * 4;
  2314. else
  2315. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2316. I915_WRITE(reg, val);
  2317. POSTING_READ(reg);
  2318. }
  2319. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2320. struct drm_i915_gem_object *obj)
  2321. {
  2322. drm_i915_private_t *dev_priv = dev->dev_private;
  2323. uint32_t val;
  2324. if (obj) {
  2325. u32 size = i915_gem_obj_ggtt_size(obj);
  2326. uint32_t pitch_val;
  2327. WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
  2328. (size & -size) != size ||
  2329. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2330. "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
  2331. i915_gem_obj_ggtt_offset(obj), size);
  2332. pitch_val = obj->stride / 128;
  2333. pitch_val = ffs(pitch_val) - 1;
  2334. val = i915_gem_obj_ggtt_offset(obj);
  2335. if (obj->tiling_mode == I915_TILING_Y)
  2336. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2337. val |= I830_FENCE_SIZE_BITS(size);
  2338. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2339. val |= I830_FENCE_REG_VALID;
  2340. } else
  2341. val = 0;
  2342. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2343. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2344. }
  2345. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2346. {
  2347. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2348. }
  2349. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2350. struct drm_i915_gem_object *obj)
  2351. {
  2352. struct drm_i915_private *dev_priv = dev->dev_private;
  2353. /* Ensure that all CPU reads are completed before installing a fence
  2354. * and all writes before removing the fence.
  2355. */
  2356. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2357. mb();
  2358. WARN(obj && (!obj->stride || !obj->tiling_mode),
  2359. "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
  2360. obj->stride, obj->tiling_mode);
  2361. switch (INTEL_INFO(dev)->gen) {
  2362. case 7:
  2363. case 6:
  2364. case 5:
  2365. case 4: i965_write_fence_reg(dev, reg, obj); break;
  2366. case 3: i915_write_fence_reg(dev, reg, obj); break;
  2367. case 2: i830_write_fence_reg(dev, reg, obj); break;
  2368. default: BUG();
  2369. }
  2370. /* And similarly be paranoid that no direct access to this region
  2371. * is reordered to before the fence is installed.
  2372. */
  2373. if (i915_gem_object_needs_mb(obj))
  2374. mb();
  2375. }
  2376. static inline int fence_number(struct drm_i915_private *dev_priv,
  2377. struct drm_i915_fence_reg *fence)
  2378. {
  2379. return fence - dev_priv->fence_regs;
  2380. }
  2381. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2382. struct drm_i915_fence_reg *fence,
  2383. bool enable)
  2384. {
  2385. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2386. int reg = fence_number(dev_priv, fence);
  2387. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  2388. if (enable) {
  2389. obj->fence_reg = reg;
  2390. fence->obj = obj;
  2391. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2392. } else {
  2393. obj->fence_reg = I915_FENCE_REG_NONE;
  2394. fence->obj = NULL;
  2395. list_del_init(&fence->lru_list);
  2396. }
  2397. obj->fence_dirty = false;
  2398. }
  2399. static int
  2400. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2401. {
  2402. if (obj->last_fenced_seqno) {
  2403. int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2404. if (ret)
  2405. return ret;
  2406. obj->last_fenced_seqno = 0;
  2407. }
  2408. obj->fenced_gpu_access = false;
  2409. return 0;
  2410. }
  2411. int
  2412. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2413. {
  2414. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2415. struct drm_i915_fence_reg *fence;
  2416. int ret;
  2417. ret = i915_gem_object_wait_fence(obj);
  2418. if (ret)
  2419. return ret;
  2420. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2421. return 0;
  2422. fence = &dev_priv->fence_regs[obj->fence_reg];
  2423. i915_gem_object_fence_lost(obj);
  2424. i915_gem_object_update_fence(obj, fence, false);
  2425. return 0;
  2426. }
  2427. static struct drm_i915_fence_reg *
  2428. i915_find_fence_reg(struct drm_device *dev)
  2429. {
  2430. struct drm_i915_private *dev_priv = dev->dev_private;
  2431. struct drm_i915_fence_reg *reg, *avail;
  2432. int i;
  2433. /* First try to find a free reg */
  2434. avail = NULL;
  2435. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2436. reg = &dev_priv->fence_regs[i];
  2437. if (!reg->obj)
  2438. return reg;
  2439. if (!reg->pin_count)
  2440. avail = reg;
  2441. }
  2442. if (avail == NULL)
  2443. return NULL;
  2444. /* None available, try to steal one or wait for a user to finish */
  2445. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2446. if (reg->pin_count)
  2447. continue;
  2448. return reg;
  2449. }
  2450. return NULL;
  2451. }
  2452. /**
  2453. * i915_gem_object_get_fence - set up fencing for an object
  2454. * @obj: object to map through a fence reg
  2455. *
  2456. * When mapping objects through the GTT, userspace wants to be able to write
  2457. * to them without having to worry about swizzling if the object is tiled.
  2458. * This function walks the fence regs looking for a free one for @obj,
  2459. * stealing one if it can't find any.
  2460. *
  2461. * It then sets up the reg based on the object's properties: address, pitch
  2462. * and tiling format.
  2463. *
  2464. * For an untiled surface, this removes any existing fence.
  2465. */
  2466. int
  2467. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2468. {
  2469. struct drm_device *dev = obj->base.dev;
  2470. struct drm_i915_private *dev_priv = dev->dev_private;
  2471. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2472. struct drm_i915_fence_reg *reg;
  2473. int ret;
  2474. /* Have we updated the tiling parameters upon the object and so
  2475. * will need to serialise the write to the associated fence register?
  2476. */
  2477. if (obj->fence_dirty) {
  2478. ret = i915_gem_object_wait_fence(obj);
  2479. if (ret)
  2480. return ret;
  2481. }
  2482. /* Just update our place in the LRU if our fence is getting reused. */
  2483. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2484. reg = &dev_priv->fence_regs[obj->fence_reg];
  2485. if (!obj->fence_dirty) {
  2486. list_move_tail(&reg->lru_list,
  2487. &dev_priv->mm.fence_list);
  2488. return 0;
  2489. }
  2490. } else if (enable) {
  2491. reg = i915_find_fence_reg(dev);
  2492. if (reg == NULL)
  2493. return -EDEADLK;
  2494. if (reg->obj) {
  2495. struct drm_i915_gem_object *old = reg->obj;
  2496. ret = i915_gem_object_wait_fence(old);
  2497. if (ret)
  2498. return ret;
  2499. i915_gem_object_fence_lost(old);
  2500. }
  2501. } else
  2502. return 0;
  2503. i915_gem_object_update_fence(obj, reg, enable);
  2504. return 0;
  2505. }
  2506. static bool i915_gem_valid_gtt_space(struct drm_device *dev,
  2507. struct drm_mm_node *gtt_space,
  2508. unsigned long cache_level)
  2509. {
  2510. struct drm_mm_node *other;
  2511. /* On non-LLC machines we have to be careful when putting differing
  2512. * types of snoopable memory together to avoid the prefetcher
  2513. * crossing memory domains and dying.
  2514. */
  2515. if (HAS_LLC(dev))
  2516. return true;
  2517. if (!drm_mm_node_allocated(gtt_space))
  2518. return true;
  2519. if (list_empty(&gtt_space->node_list))
  2520. return true;
  2521. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2522. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2523. return false;
  2524. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2525. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2526. return false;
  2527. return true;
  2528. }
  2529. static void i915_gem_verify_gtt(struct drm_device *dev)
  2530. {
  2531. #if WATCH_GTT
  2532. struct drm_i915_private *dev_priv = dev->dev_private;
  2533. struct drm_i915_gem_object *obj;
  2534. int err = 0;
  2535. list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
  2536. if (obj->gtt_space == NULL) {
  2537. printk(KERN_ERR "object found on GTT list with no space reserved\n");
  2538. err++;
  2539. continue;
  2540. }
  2541. if (obj->cache_level != obj->gtt_space->color) {
  2542. printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
  2543. i915_gem_obj_ggtt_offset(obj),
  2544. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2545. obj->cache_level,
  2546. obj->gtt_space->color);
  2547. err++;
  2548. continue;
  2549. }
  2550. if (!i915_gem_valid_gtt_space(dev,
  2551. obj->gtt_space,
  2552. obj->cache_level)) {
  2553. printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
  2554. i915_gem_obj_ggtt_offset(obj),
  2555. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2556. obj->cache_level);
  2557. err++;
  2558. continue;
  2559. }
  2560. }
  2561. WARN_ON(err);
  2562. #endif
  2563. }
  2564. /**
  2565. * Finds free space in the GTT aperture and binds the object there.
  2566. */
  2567. static int
  2568. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  2569. struct i915_address_space *vm,
  2570. unsigned alignment,
  2571. bool map_and_fenceable,
  2572. bool nonblocking)
  2573. {
  2574. struct drm_device *dev = obj->base.dev;
  2575. drm_i915_private_t *dev_priv = dev->dev_private;
  2576. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2577. bool mappable, fenceable;
  2578. size_t gtt_max =
  2579. map_and_fenceable ? dev_priv->gtt.mappable_end : vm->total;
  2580. struct i915_vma *vma;
  2581. int ret;
  2582. if (WARN_ON(!list_empty(&obj->vma_list)))
  2583. return -EBUSY;
  2584. fence_size = i915_gem_get_gtt_size(dev,
  2585. obj->base.size,
  2586. obj->tiling_mode);
  2587. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2588. obj->base.size,
  2589. obj->tiling_mode, true);
  2590. unfenced_alignment =
  2591. i915_gem_get_gtt_alignment(dev,
  2592. obj->base.size,
  2593. obj->tiling_mode, false);
  2594. if (alignment == 0)
  2595. alignment = map_and_fenceable ? fence_alignment :
  2596. unfenced_alignment;
  2597. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2598. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2599. return -EINVAL;
  2600. }
  2601. size = map_and_fenceable ? fence_size : obj->base.size;
  2602. /* If the object is bigger than the entire aperture, reject it early
  2603. * before evicting everything in a vain attempt to find space.
  2604. */
  2605. if (obj->base.size > gtt_max) {
  2606. DRM_ERROR("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n",
  2607. obj->base.size,
  2608. map_and_fenceable ? "mappable" : "total",
  2609. gtt_max);
  2610. return -E2BIG;
  2611. }
  2612. ret = i915_gem_object_get_pages(obj);
  2613. if (ret)
  2614. return ret;
  2615. i915_gem_object_pin_pages(obj);
  2616. /* FIXME: For now we only ever use 1 VMA per object */
  2617. BUG_ON(!i915_is_ggtt(vm));
  2618. WARN_ON(!list_empty(&obj->vma_list));
  2619. vma = i915_gem_vma_create(obj, vm);
  2620. if (IS_ERR(vma)) {
  2621. ret = PTR_ERR(vma);
  2622. goto err_unpin;
  2623. }
  2624. search_free:
  2625. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  2626. size, alignment,
  2627. obj->cache_level, 0, gtt_max);
  2628. if (ret) {
  2629. ret = i915_gem_evict_something(dev, vm, size, alignment,
  2630. obj->cache_level,
  2631. map_and_fenceable,
  2632. nonblocking);
  2633. if (ret == 0)
  2634. goto search_free;
  2635. goto err_free_vma;
  2636. }
  2637. if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node,
  2638. obj->cache_level))) {
  2639. ret = -EINVAL;
  2640. goto err_remove_node;
  2641. }
  2642. ret = i915_gem_gtt_prepare_object(obj);
  2643. if (ret)
  2644. goto err_remove_node;
  2645. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2646. list_add_tail(&obj->mm_list, &vm->inactive_list);
  2647. /* Keep GGTT vmas first to make debug easier */
  2648. if (i915_is_ggtt(vm))
  2649. list_add(&vma->vma_link, &obj->vma_list);
  2650. else
  2651. list_add_tail(&vma->vma_link, &obj->vma_list);
  2652. fenceable =
  2653. i915_is_ggtt(vm) &&
  2654. i915_gem_obj_ggtt_size(obj) == fence_size &&
  2655. (i915_gem_obj_ggtt_offset(obj) & (fence_alignment - 1)) == 0;
  2656. mappable =
  2657. i915_is_ggtt(vm) &&
  2658. vma->node.start + obj->base.size <= dev_priv->gtt.mappable_end;
  2659. /* Map and fenceable only changes if the VM is the global GGTT */
  2660. if (i915_is_ggtt(vm))
  2661. obj->map_and_fenceable = mappable && fenceable;
  2662. trace_i915_vma_bind(vma, map_and_fenceable);
  2663. i915_gem_verify_gtt(dev);
  2664. return 0;
  2665. err_remove_node:
  2666. drm_mm_remove_node(&vma->node);
  2667. err_free_vma:
  2668. i915_gem_vma_destroy(vma);
  2669. err_unpin:
  2670. i915_gem_object_unpin_pages(obj);
  2671. return ret;
  2672. }
  2673. void
  2674. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2675. {
  2676. /* If we don't have a page list set up, then we're not pinned
  2677. * to GPU, and we can ignore the cache flush because it'll happen
  2678. * again at bind time.
  2679. */
  2680. if (obj->pages == NULL)
  2681. return;
  2682. /*
  2683. * Stolen memory is always coherent with the GPU as it is explicitly
  2684. * marked as wc by the system, or the system is cache-coherent.
  2685. */
  2686. if (obj->stolen)
  2687. return;
  2688. /* If the GPU is snooping the contents of the CPU cache,
  2689. * we do not need to manually clear the CPU cache lines. However,
  2690. * the caches are only snooped when the render cache is
  2691. * flushed/invalidated. As we always have to emit invalidations
  2692. * and flushes when moving into and out of the RENDER domain, correct
  2693. * snooping behaviour occurs naturally as the result of our domain
  2694. * tracking.
  2695. */
  2696. if (obj->cache_level != I915_CACHE_NONE)
  2697. return;
  2698. trace_i915_gem_object_clflush(obj);
  2699. drm_clflush_sg(obj->pages);
  2700. }
  2701. /** Flushes the GTT write domain for the object if it's dirty. */
  2702. static void
  2703. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2704. {
  2705. uint32_t old_write_domain;
  2706. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2707. return;
  2708. /* No actual flushing is required for the GTT write domain. Writes
  2709. * to it immediately go to main memory as far as we know, so there's
  2710. * no chipset flush. It also doesn't land in render cache.
  2711. *
  2712. * However, we do have to enforce the order so that all writes through
  2713. * the GTT land before any writes to the device, such as updates to
  2714. * the GATT itself.
  2715. */
  2716. wmb();
  2717. old_write_domain = obj->base.write_domain;
  2718. obj->base.write_domain = 0;
  2719. trace_i915_gem_object_change_domain(obj,
  2720. obj->base.read_domains,
  2721. old_write_domain);
  2722. }
  2723. /** Flushes the CPU write domain for the object if it's dirty. */
  2724. static void
  2725. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2726. {
  2727. uint32_t old_write_domain;
  2728. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2729. return;
  2730. i915_gem_clflush_object(obj);
  2731. i915_gem_chipset_flush(obj->base.dev);
  2732. old_write_domain = obj->base.write_domain;
  2733. obj->base.write_domain = 0;
  2734. trace_i915_gem_object_change_domain(obj,
  2735. obj->base.read_domains,
  2736. old_write_domain);
  2737. }
  2738. /**
  2739. * Moves a single object to the GTT read, and possibly write domain.
  2740. *
  2741. * This function returns when the move is complete, including waiting on
  2742. * flushes to occur.
  2743. */
  2744. int
  2745. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2746. {
  2747. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2748. uint32_t old_write_domain, old_read_domains;
  2749. int ret;
  2750. /* Not valid to be called on unbound objects. */
  2751. if (!i915_gem_obj_bound_any(obj))
  2752. return -EINVAL;
  2753. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2754. return 0;
  2755. ret = i915_gem_object_wait_rendering(obj, !write);
  2756. if (ret)
  2757. return ret;
  2758. i915_gem_object_flush_cpu_write_domain(obj);
  2759. /* Serialise direct access to this object with the barriers for
  2760. * coherent writes from the GPU, by effectively invalidating the
  2761. * GTT domain upon first access.
  2762. */
  2763. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2764. mb();
  2765. old_write_domain = obj->base.write_domain;
  2766. old_read_domains = obj->base.read_domains;
  2767. /* It should now be out of any other write domains, and we can update
  2768. * the domain values for our changes.
  2769. */
  2770. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2771. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2772. if (write) {
  2773. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2774. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2775. obj->dirty = 1;
  2776. }
  2777. trace_i915_gem_object_change_domain(obj,
  2778. old_read_domains,
  2779. old_write_domain);
  2780. /* And bump the LRU for this access */
  2781. if (i915_gem_object_is_inactive(obj))
  2782. list_move_tail(&obj->mm_list,
  2783. &dev_priv->gtt.base.inactive_list);
  2784. return 0;
  2785. }
  2786. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2787. enum i915_cache_level cache_level)
  2788. {
  2789. struct drm_device *dev = obj->base.dev;
  2790. drm_i915_private_t *dev_priv = dev->dev_private;
  2791. struct i915_vma *vma;
  2792. int ret;
  2793. if (obj->cache_level == cache_level)
  2794. return 0;
  2795. if (obj->pin_count) {
  2796. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2797. return -EBUSY;
  2798. }
  2799. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  2800. if (!i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) {
  2801. ret = i915_vma_unbind(vma);
  2802. if (ret)
  2803. return ret;
  2804. break;
  2805. }
  2806. }
  2807. if (i915_gem_obj_bound_any(obj)) {
  2808. ret = i915_gem_object_finish_gpu(obj);
  2809. if (ret)
  2810. return ret;
  2811. i915_gem_object_finish_gtt(obj);
  2812. /* Before SandyBridge, you could not use tiling or fence
  2813. * registers with snooped memory, so relinquish any fences
  2814. * currently pointing to our region in the aperture.
  2815. */
  2816. if (INTEL_INFO(dev)->gen < 6) {
  2817. ret = i915_gem_object_put_fence(obj);
  2818. if (ret)
  2819. return ret;
  2820. }
  2821. if (obj->has_global_gtt_mapping)
  2822. i915_gem_gtt_bind_object(obj, cache_level);
  2823. if (obj->has_aliasing_ppgtt_mapping)
  2824. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2825. obj, cache_level);
  2826. }
  2827. if (cache_level == I915_CACHE_NONE) {
  2828. u32 old_read_domains, old_write_domain;
  2829. /* If we're coming from LLC cached, then we haven't
  2830. * actually been tracking whether the data is in the
  2831. * CPU cache or not, since we only allow one bit set
  2832. * in obj->write_domain and have been skipping the clflushes.
  2833. * Just set it to the CPU cache for now.
  2834. */
  2835. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2836. WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
  2837. old_read_domains = obj->base.read_domains;
  2838. old_write_domain = obj->base.write_domain;
  2839. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2840. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2841. trace_i915_gem_object_change_domain(obj,
  2842. old_read_domains,
  2843. old_write_domain);
  2844. }
  2845. list_for_each_entry(vma, &obj->vma_list, vma_link)
  2846. vma->node.color = cache_level;
  2847. obj->cache_level = cache_level;
  2848. i915_gem_verify_gtt(dev);
  2849. return 0;
  2850. }
  2851. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2852. struct drm_file *file)
  2853. {
  2854. struct drm_i915_gem_caching *args = data;
  2855. struct drm_i915_gem_object *obj;
  2856. int ret;
  2857. ret = i915_mutex_lock_interruptible(dev);
  2858. if (ret)
  2859. return ret;
  2860. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2861. if (&obj->base == NULL) {
  2862. ret = -ENOENT;
  2863. goto unlock;
  2864. }
  2865. args->caching = obj->cache_level != I915_CACHE_NONE;
  2866. drm_gem_object_unreference(&obj->base);
  2867. unlock:
  2868. mutex_unlock(&dev->struct_mutex);
  2869. return ret;
  2870. }
  2871. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2872. struct drm_file *file)
  2873. {
  2874. struct drm_i915_gem_caching *args = data;
  2875. struct drm_i915_gem_object *obj;
  2876. enum i915_cache_level level;
  2877. int ret;
  2878. switch (args->caching) {
  2879. case I915_CACHING_NONE:
  2880. level = I915_CACHE_NONE;
  2881. break;
  2882. case I915_CACHING_CACHED:
  2883. level = I915_CACHE_LLC;
  2884. break;
  2885. default:
  2886. return -EINVAL;
  2887. }
  2888. ret = i915_mutex_lock_interruptible(dev);
  2889. if (ret)
  2890. return ret;
  2891. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2892. if (&obj->base == NULL) {
  2893. ret = -ENOENT;
  2894. goto unlock;
  2895. }
  2896. ret = i915_gem_object_set_cache_level(obj, level);
  2897. drm_gem_object_unreference(&obj->base);
  2898. unlock:
  2899. mutex_unlock(&dev->struct_mutex);
  2900. return ret;
  2901. }
  2902. /*
  2903. * Prepare buffer for display plane (scanout, cursors, etc).
  2904. * Can be called from an uninterruptible phase (modesetting) and allows
  2905. * any flushes to be pipelined (for pageflips).
  2906. */
  2907. int
  2908. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2909. u32 alignment,
  2910. struct intel_ring_buffer *pipelined)
  2911. {
  2912. u32 old_read_domains, old_write_domain;
  2913. int ret;
  2914. if (pipelined != obj->ring) {
  2915. ret = i915_gem_object_sync(obj, pipelined);
  2916. if (ret)
  2917. return ret;
  2918. }
  2919. /* The display engine is not coherent with the LLC cache on gen6. As
  2920. * a result, we make sure that the pinning that is about to occur is
  2921. * done with uncached PTEs. This is lowest common denominator for all
  2922. * chipsets.
  2923. *
  2924. * However for gen6+, we could do better by using the GFDT bit instead
  2925. * of uncaching, which would allow us to flush all the LLC-cached data
  2926. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2927. */
  2928. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
  2929. if (ret)
  2930. return ret;
  2931. /* As the user may map the buffer once pinned in the display plane
  2932. * (e.g. libkms for the bootup splash), we have to ensure that we
  2933. * always use map_and_fenceable for all scanout buffers.
  2934. */
  2935. ret = i915_gem_obj_ggtt_pin(obj, alignment, true, false);
  2936. if (ret)
  2937. return ret;
  2938. i915_gem_object_flush_cpu_write_domain(obj);
  2939. old_write_domain = obj->base.write_domain;
  2940. old_read_domains = obj->base.read_domains;
  2941. /* It should now be out of any other write domains, and we can update
  2942. * the domain values for our changes.
  2943. */
  2944. obj->base.write_domain = 0;
  2945. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2946. trace_i915_gem_object_change_domain(obj,
  2947. old_read_domains,
  2948. old_write_domain);
  2949. return 0;
  2950. }
  2951. int
  2952. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  2953. {
  2954. int ret;
  2955. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  2956. return 0;
  2957. ret = i915_gem_object_wait_rendering(obj, false);
  2958. if (ret)
  2959. return ret;
  2960. /* Ensure that we invalidate the GPU's caches and TLBs. */
  2961. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  2962. return 0;
  2963. }
  2964. /**
  2965. * Moves a single object to the CPU read, and possibly write domain.
  2966. *
  2967. * This function returns when the move is complete, including waiting on
  2968. * flushes to occur.
  2969. */
  2970. int
  2971. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2972. {
  2973. uint32_t old_write_domain, old_read_domains;
  2974. int ret;
  2975. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  2976. return 0;
  2977. ret = i915_gem_object_wait_rendering(obj, !write);
  2978. if (ret)
  2979. return ret;
  2980. i915_gem_object_flush_gtt_write_domain(obj);
  2981. old_write_domain = obj->base.write_domain;
  2982. old_read_domains = obj->base.read_domains;
  2983. /* Flush the CPU cache if it's still invalid. */
  2984. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2985. i915_gem_clflush_object(obj);
  2986. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2987. }
  2988. /* It should now be out of any other write domains, and we can update
  2989. * the domain values for our changes.
  2990. */
  2991. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2992. /* If we're writing through the CPU, then the GPU read domains will
  2993. * need to be invalidated at next use.
  2994. */
  2995. if (write) {
  2996. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2997. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2998. }
  2999. trace_i915_gem_object_change_domain(obj,
  3000. old_read_domains,
  3001. old_write_domain);
  3002. return 0;
  3003. }
  3004. /* Throttle our rendering by waiting until the ring has completed our requests
  3005. * emitted over 20 msec ago.
  3006. *
  3007. * Note that if we were to use the current jiffies each time around the loop,
  3008. * we wouldn't escape the function with any frames outstanding if the time to
  3009. * render a frame was over 20ms.
  3010. *
  3011. * This should get us reasonable parallelism between CPU and GPU but also
  3012. * relatively low latency when blocking on a particular request to finish.
  3013. */
  3014. static int
  3015. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3016. {
  3017. struct drm_i915_private *dev_priv = dev->dev_private;
  3018. struct drm_i915_file_private *file_priv = file->driver_priv;
  3019. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  3020. struct drm_i915_gem_request *request;
  3021. struct intel_ring_buffer *ring = NULL;
  3022. unsigned reset_counter;
  3023. u32 seqno = 0;
  3024. int ret;
  3025. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3026. if (ret)
  3027. return ret;
  3028. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  3029. if (ret)
  3030. return ret;
  3031. spin_lock(&file_priv->mm.lock);
  3032. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3033. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3034. break;
  3035. ring = request->ring;
  3036. seqno = request->seqno;
  3037. }
  3038. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3039. spin_unlock(&file_priv->mm.lock);
  3040. if (seqno == 0)
  3041. return 0;
  3042. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
  3043. if (ret == 0)
  3044. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3045. return ret;
  3046. }
  3047. int
  3048. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3049. struct i915_address_space *vm,
  3050. uint32_t alignment,
  3051. bool map_and_fenceable,
  3052. bool nonblocking)
  3053. {
  3054. struct i915_vma *vma;
  3055. int ret;
  3056. if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3057. return -EBUSY;
  3058. WARN_ON(map_and_fenceable && !i915_is_ggtt(vm));
  3059. vma = i915_gem_obj_to_vma(obj, vm);
  3060. if (vma) {
  3061. if ((alignment &&
  3062. vma->node.start & (alignment - 1)) ||
  3063. (map_and_fenceable && !obj->map_and_fenceable)) {
  3064. WARN(obj->pin_count,
  3065. "bo is already pinned with incorrect alignment:"
  3066. " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
  3067. " obj->map_and_fenceable=%d\n",
  3068. i915_gem_obj_offset(obj, vm), alignment,
  3069. map_and_fenceable,
  3070. obj->map_and_fenceable);
  3071. ret = i915_vma_unbind(vma);
  3072. if (ret)
  3073. return ret;
  3074. }
  3075. }
  3076. if (!i915_gem_obj_bound(obj, vm)) {
  3077. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3078. ret = i915_gem_object_bind_to_vm(obj, vm, alignment,
  3079. map_and_fenceable,
  3080. nonblocking);
  3081. if (ret)
  3082. return ret;
  3083. if (!dev_priv->mm.aliasing_ppgtt)
  3084. i915_gem_gtt_bind_object(obj, obj->cache_level);
  3085. }
  3086. if (!obj->has_global_gtt_mapping && map_and_fenceable)
  3087. i915_gem_gtt_bind_object(obj, obj->cache_level);
  3088. obj->pin_count++;
  3089. obj->pin_mappable |= map_and_fenceable;
  3090. return 0;
  3091. }
  3092. void
  3093. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  3094. {
  3095. BUG_ON(obj->pin_count == 0);
  3096. BUG_ON(!i915_gem_obj_bound_any(obj));
  3097. if (--obj->pin_count == 0)
  3098. obj->pin_mappable = false;
  3099. }
  3100. int
  3101. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3102. struct drm_file *file)
  3103. {
  3104. struct drm_i915_gem_pin *args = data;
  3105. struct drm_i915_gem_object *obj;
  3106. int ret;
  3107. ret = i915_mutex_lock_interruptible(dev);
  3108. if (ret)
  3109. return ret;
  3110. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3111. if (&obj->base == NULL) {
  3112. ret = -ENOENT;
  3113. goto unlock;
  3114. }
  3115. if (obj->madv != I915_MADV_WILLNEED) {
  3116. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3117. ret = -EINVAL;
  3118. goto out;
  3119. }
  3120. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  3121. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3122. args->handle);
  3123. ret = -EINVAL;
  3124. goto out;
  3125. }
  3126. if (obj->user_pin_count == 0) {
  3127. ret = i915_gem_obj_ggtt_pin(obj, args->alignment, true, false);
  3128. if (ret)
  3129. goto out;
  3130. }
  3131. obj->user_pin_count++;
  3132. obj->pin_filp = file;
  3133. /* XXX - flush the CPU caches for pinned objects
  3134. * as the X server doesn't manage domains yet
  3135. */
  3136. i915_gem_object_flush_cpu_write_domain(obj);
  3137. args->offset = i915_gem_obj_ggtt_offset(obj);
  3138. out:
  3139. drm_gem_object_unreference(&obj->base);
  3140. unlock:
  3141. mutex_unlock(&dev->struct_mutex);
  3142. return ret;
  3143. }
  3144. int
  3145. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3146. struct drm_file *file)
  3147. {
  3148. struct drm_i915_gem_pin *args = data;
  3149. struct drm_i915_gem_object *obj;
  3150. int ret;
  3151. ret = i915_mutex_lock_interruptible(dev);
  3152. if (ret)
  3153. return ret;
  3154. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3155. if (&obj->base == NULL) {
  3156. ret = -ENOENT;
  3157. goto unlock;
  3158. }
  3159. if (obj->pin_filp != file) {
  3160. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3161. args->handle);
  3162. ret = -EINVAL;
  3163. goto out;
  3164. }
  3165. obj->user_pin_count--;
  3166. if (obj->user_pin_count == 0) {
  3167. obj->pin_filp = NULL;
  3168. i915_gem_object_unpin(obj);
  3169. }
  3170. out:
  3171. drm_gem_object_unreference(&obj->base);
  3172. unlock:
  3173. mutex_unlock(&dev->struct_mutex);
  3174. return ret;
  3175. }
  3176. int
  3177. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3178. struct drm_file *file)
  3179. {
  3180. struct drm_i915_gem_busy *args = data;
  3181. struct drm_i915_gem_object *obj;
  3182. int ret;
  3183. ret = i915_mutex_lock_interruptible(dev);
  3184. if (ret)
  3185. return ret;
  3186. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3187. if (&obj->base == NULL) {
  3188. ret = -ENOENT;
  3189. goto unlock;
  3190. }
  3191. /* Count all active objects as busy, even if they are currently not used
  3192. * by the gpu. Users of this interface expect objects to eventually
  3193. * become non-busy without any further actions, therefore emit any
  3194. * necessary flushes here.
  3195. */
  3196. ret = i915_gem_object_flush_active(obj);
  3197. args->busy = obj->active;
  3198. if (obj->ring) {
  3199. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3200. args->busy |= intel_ring_flag(obj->ring) << 16;
  3201. }
  3202. drm_gem_object_unreference(&obj->base);
  3203. unlock:
  3204. mutex_unlock(&dev->struct_mutex);
  3205. return ret;
  3206. }
  3207. int
  3208. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3209. struct drm_file *file_priv)
  3210. {
  3211. return i915_gem_ring_throttle(dev, file_priv);
  3212. }
  3213. int
  3214. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3215. struct drm_file *file_priv)
  3216. {
  3217. struct drm_i915_gem_madvise *args = data;
  3218. struct drm_i915_gem_object *obj;
  3219. int ret;
  3220. switch (args->madv) {
  3221. case I915_MADV_DONTNEED:
  3222. case I915_MADV_WILLNEED:
  3223. break;
  3224. default:
  3225. return -EINVAL;
  3226. }
  3227. ret = i915_mutex_lock_interruptible(dev);
  3228. if (ret)
  3229. return ret;
  3230. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3231. if (&obj->base == NULL) {
  3232. ret = -ENOENT;
  3233. goto unlock;
  3234. }
  3235. if (obj->pin_count) {
  3236. ret = -EINVAL;
  3237. goto out;
  3238. }
  3239. if (obj->madv != __I915_MADV_PURGED)
  3240. obj->madv = args->madv;
  3241. /* if the object is no longer attached, discard its backing storage */
  3242. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3243. i915_gem_object_truncate(obj);
  3244. args->retained = obj->madv != __I915_MADV_PURGED;
  3245. out:
  3246. drm_gem_object_unreference(&obj->base);
  3247. unlock:
  3248. mutex_unlock(&dev->struct_mutex);
  3249. return ret;
  3250. }
  3251. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3252. const struct drm_i915_gem_object_ops *ops)
  3253. {
  3254. INIT_LIST_HEAD(&obj->mm_list);
  3255. INIT_LIST_HEAD(&obj->global_list);
  3256. INIT_LIST_HEAD(&obj->ring_list);
  3257. INIT_LIST_HEAD(&obj->exec_list);
  3258. INIT_LIST_HEAD(&obj->vma_list);
  3259. obj->ops = ops;
  3260. obj->fence_reg = I915_FENCE_REG_NONE;
  3261. obj->madv = I915_MADV_WILLNEED;
  3262. /* Avoid an unnecessary call to unbind on the first bind. */
  3263. obj->map_and_fenceable = true;
  3264. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3265. }
  3266. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3267. .get_pages = i915_gem_object_get_pages_gtt,
  3268. .put_pages = i915_gem_object_put_pages_gtt,
  3269. };
  3270. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3271. size_t size)
  3272. {
  3273. struct drm_i915_gem_object *obj;
  3274. struct address_space *mapping;
  3275. gfp_t mask;
  3276. obj = i915_gem_object_alloc(dev);
  3277. if (obj == NULL)
  3278. return NULL;
  3279. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3280. i915_gem_object_free(obj);
  3281. return NULL;
  3282. }
  3283. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3284. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3285. /* 965gm cannot relocate objects above 4GiB. */
  3286. mask &= ~__GFP_HIGHMEM;
  3287. mask |= __GFP_DMA32;
  3288. }
  3289. mapping = file_inode(obj->base.filp)->i_mapping;
  3290. mapping_set_gfp_mask(mapping, mask);
  3291. i915_gem_object_init(obj, &i915_gem_object_ops);
  3292. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3293. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3294. if (HAS_LLC(dev)) {
  3295. /* On some devices, we can have the GPU use the LLC (the CPU
  3296. * cache) for about a 10% performance improvement
  3297. * compared to uncached. Graphics requests other than
  3298. * display scanout are coherent with the CPU in
  3299. * accessing this cache. This means in this mode we
  3300. * don't need to clflush on the CPU side, and on the
  3301. * GPU side we only need to flush internal caches to
  3302. * get data visible to the CPU.
  3303. *
  3304. * However, we maintain the display planes as UC, and so
  3305. * need to rebind when first used as such.
  3306. */
  3307. obj->cache_level = I915_CACHE_LLC;
  3308. } else
  3309. obj->cache_level = I915_CACHE_NONE;
  3310. trace_i915_gem_object_create(obj);
  3311. return obj;
  3312. }
  3313. int i915_gem_init_object(struct drm_gem_object *obj)
  3314. {
  3315. BUG();
  3316. return 0;
  3317. }
  3318. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3319. {
  3320. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3321. struct drm_device *dev = obj->base.dev;
  3322. drm_i915_private_t *dev_priv = dev->dev_private;
  3323. struct i915_vma *vma, *next;
  3324. trace_i915_gem_object_destroy(obj);
  3325. if (obj->phys_obj)
  3326. i915_gem_detach_phys_object(dev, obj);
  3327. obj->pin_count = 0;
  3328. /* NB: 0 or 1 elements */
  3329. WARN_ON(!list_empty(&obj->vma_list) &&
  3330. !list_is_singular(&obj->vma_list));
  3331. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3332. int ret = i915_vma_unbind(vma);
  3333. if (WARN_ON(ret == -ERESTARTSYS)) {
  3334. bool was_interruptible;
  3335. was_interruptible = dev_priv->mm.interruptible;
  3336. dev_priv->mm.interruptible = false;
  3337. WARN_ON(i915_vma_unbind(vma));
  3338. dev_priv->mm.interruptible = was_interruptible;
  3339. }
  3340. }
  3341. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3342. * before progressing. */
  3343. if (obj->stolen)
  3344. i915_gem_object_unpin_pages(obj);
  3345. if (WARN_ON(obj->pages_pin_count))
  3346. obj->pages_pin_count = 0;
  3347. i915_gem_object_put_pages(obj);
  3348. i915_gem_object_free_mmap_offset(obj);
  3349. i915_gem_object_release_stolen(obj);
  3350. BUG_ON(obj->pages);
  3351. if (obj->base.import_attach)
  3352. drm_prime_gem_destroy(&obj->base, NULL);
  3353. drm_gem_object_release(&obj->base);
  3354. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3355. kfree(obj->bit_17);
  3356. i915_gem_object_free(obj);
  3357. }
  3358. struct i915_vma *i915_gem_vma_create(struct drm_i915_gem_object *obj,
  3359. struct i915_address_space *vm)
  3360. {
  3361. struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
  3362. if (vma == NULL)
  3363. return ERR_PTR(-ENOMEM);
  3364. INIT_LIST_HEAD(&vma->vma_link);
  3365. vma->vm = vm;
  3366. vma->obj = obj;
  3367. return vma;
  3368. }
  3369. void i915_gem_vma_destroy(struct i915_vma *vma)
  3370. {
  3371. WARN_ON(vma->node.allocated);
  3372. kfree(vma);
  3373. }
  3374. int
  3375. i915_gem_idle(struct drm_device *dev)
  3376. {
  3377. drm_i915_private_t *dev_priv = dev->dev_private;
  3378. int ret;
  3379. if (dev_priv->ums.mm_suspended) {
  3380. mutex_unlock(&dev->struct_mutex);
  3381. return 0;
  3382. }
  3383. ret = i915_gpu_idle(dev);
  3384. if (ret) {
  3385. mutex_unlock(&dev->struct_mutex);
  3386. return ret;
  3387. }
  3388. i915_gem_retire_requests(dev);
  3389. /* Under UMS, be paranoid and evict. */
  3390. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3391. i915_gem_evict_everything(dev);
  3392. del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
  3393. i915_kernel_lost_context(dev);
  3394. i915_gem_cleanup_ringbuffer(dev);
  3395. /* Cancel the retire work handler, which should be idle now. */
  3396. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3397. return 0;
  3398. }
  3399. void i915_gem_l3_remap(struct drm_device *dev)
  3400. {
  3401. drm_i915_private_t *dev_priv = dev->dev_private;
  3402. u32 misccpctl;
  3403. int i;
  3404. if (!HAS_L3_GPU_CACHE(dev))
  3405. return;
  3406. if (!dev_priv->l3_parity.remap_info)
  3407. return;
  3408. misccpctl = I915_READ(GEN7_MISCCPCTL);
  3409. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  3410. POSTING_READ(GEN7_MISCCPCTL);
  3411. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3412. u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
  3413. if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
  3414. DRM_DEBUG("0x%x was already programmed to %x\n",
  3415. GEN7_L3LOG_BASE + i, remap);
  3416. if (remap && !dev_priv->l3_parity.remap_info[i/4])
  3417. DRM_DEBUG_DRIVER("Clearing remapped register\n");
  3418. I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
  3419. }
  3420. /* Make sure all the writes land before disabling dop clock gating */
  3421. POSTING_READ(GEN7_L3LOG_BASE);
  3422. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  3423. }
  3424. void i915_gem_init_swizzling(struct drm_device *dev)
  3425. {
  3426. drm_i915_private_t *dev_priv = dev->dev_private;
  3427. if (INTEL_INFO(dev)->gen < 5 ||
  3428. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3429. return;
  3430. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3431. DISP_TILE_SURFACE_SWIZZLING);
  3432. if (IS_GEN5(dev))
  3433. return;
  3434. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3435. if (IS_GEN6(dev))
  3436. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3437. else if (IS_GEN7(dev))
  3438. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3439. else
  3440. BUG();
  3441. }
  3442. static bool
  3443. intel_enable_blt(struct drm_device *dev)
  3444. {
  3445. if (!HAS_BLT(dev))
  3446. return false;
  3447. /* The blitter was dysfunctional on early prototypes */
  3448. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3449. DRM_INFO("BLT not supported on this pre-production hardware;"
  3450. " graphics performance will be degraded.\n");
  3451. return false;
  3452. }
  3453. return true;
  3454. }
  3455. static int i915_gem_init_rings(struct drm_device *dev)
  3456. {
  3457. struct drm_i915_private *dev_priv = dev->dev_private;
  3458. int ret;
  3459. ret = intel_init_render_ring_buffer(dev);
  3460. if (ret)
  3461. return ret;
  3462. if (HAS_BSD(dev)) {
  3463. ret = intel_init_bsd_ring_buffer(dev);
  3464. if (ret)
  3465. goto cleanup_render_ring;
  3466. }
  3467. if (intel_enable_blt(dev)) {
  3468. ret = intel_init_blt_ring_buffer(dev);
  3469. if (ret)
  3470. goto cleanup_bsd_ring;
  3471. }
  3472. if (HAS_VEBOX(dev)) {
  3473. ret = intel_init_vebox_ring_buffer(dev);
  3474. if (ret)
  3475. goto cleanup_blt_ring;
  3476. }
  3477. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  3478. if (ret)
  3479. goto cleanup_vebox_ring;
  3480. return 0;
  3481. cleanup_vebox_ring:
  3482. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  3483. cleanup_blt_ring:
  3484. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3485. cleanup_bsd_ring:
  3486. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3487. cleanup_render_ring:
  3488. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3489. return ret;
  3490. }
  3491. int
  3492. i915_gem_init_hw(struct drm_device *dev)
  3493. {
  3494. drm_i915_private_t *dev_priv = dev->dev_private;
  3495. int ret;
  3496. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3497. return -EIO;
  3498. if (dev_priv->ellc_size)
  3499. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3500. if (HAS_PCH_NOP(dev)) {
  3501. u32 temp = I915_READ(GEN7_MSG_CTL);
  3502. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3503. I915_WRITE(GEN7_MSG_CTL, temp);
  3504. }
  3505. i915_gem_l3_remap(dev);
  3506. i915_gem_init_swizzling(dev);
  3507. ret = i915_gem_init_rings(dev);
  3508. if (ret)
  3509. return ret;
  3510. /*
  3511. * XXX: There was some w/a described somewhere suggesting loading
  3512. * contexts before PPGTT.
  3513. */
  3514. i915_gem_context_init(dev);
  3515. if (dev_priv->mm.aliasing_ppgtt) {
  3516. ret = dev_priv->mm.aliasing_ppgtt->enable(dev);
  3517. if (ret) {
  3518. i915_gem_cleanup_aliasing_ppgtt(dev);
  3519. DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n");
  3520. }
  3521. }
  3522. return 0;
  3523. }
  3524. int i915_gem_init(struct drm_device *dev)
  3525. {
  3526. struct drm_i915_private *dev_priv = dev->dev_private;
  3527. int ret;
  3528. mutex_lock(&dev->struct_mutex);
  3529. if (IS_VALLEYVIEW(dev)) {
  3530. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  3531. I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
  3532. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
  3533. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  3534. }
  3535. i915_gem_init_global_gtt(dev);
  3536. ret = i915_gem_init_hw(dev);
  3537. mutex_unlock(&dev->struct_mutex);
  3538. if (ret) {
  3539. i915_gem_cleanup_aliasing_ppgtt(dev);
  3540. return ret;
  3541. }
  3542. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  3543. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3544. dev_priv->dri1.allow_batchbuffer = 1;
  3545. return 0;
  3546. }
  3547. void
  3548. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3549. {
  3550. drm_i915_private_t *dev_priv = dev->dev_private;
  3551. struct intel_ring_buffer *ring;
  3552. int i;
  3553. for_each_ring(ring, dev_priv, i)
  3554. intel_cleanup_ring_buffer(ring);
  3555. }
  3556. int
  3557. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3558. struct drm_file *file_priv)
  3559. {
  3560. struct drm_i915_private *dev_priv = dev->dev_private;
  3561. int ret;
  3562. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3563. return 0;
  3564. if (i915_reset_in_progress(&dev_priv->gpu_error)) {
  3565. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3566. atomic_set(&dev_priv->gpu_error.reset_counter, 0);
  3567. }
  3568. mutex_lock(&dev->struct_mutex);
  3569. dev_priv->ums.mm_suspended = 0;
  3570. ret = i915_gem_init_hw(dev);
  3571. if (ret != 0) {
  3572. mutex_unlock(&dev->struct_mutex);
  3573. return ret;
  3574. }
  3575. BUG_ON(!list_empty(&dev_priv->gtt.base.active_list));
  3576. mutex_unlock(&dev->struct_mutex);
  3577. ret = drm_irq_install(dev);
  3578. if (ret)
  3579. goto cleanup_ringbuffer;
  3580. return 0;
  3581. cleanup_ringbuffer:
  3582. mutex_lock(&dev->struct_mutex);
  3583. i915_gem_cleanup_ringbuffer(dev);
  3584. dev_priv->ums.mm_suspended = 1;
  3585. mutex_unlock(&dev->struct_mutex);
  3586. return ret;
  3587. }
  3588. int
  3589. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3590. struct drm_file *file_priv)
  3591. {
  3592. struct drm_i915_private *dev_priv = dev->dev_private;
  3593. int ret;
  3594. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3595. return 0;
  3596. drm_irq_uninstall(dev);
  3597. mutex_lock(&dev->struct_mutex);
  3598. ret = i915_gem_idle(dev);
  3599. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3600. * We need to replace this with a semaphore, or something.
  3601. * And not confound ums.mm_suspended!
  3602. */
  3603. if (ret != 0)
  3604. dev_priv->ums.mm_suspended = 1;
  3605. mutex_unlock(&dev->struct_mutex);
  3606. return ret;
  3607. }
  3608. void
  3609. i915_gem_lastclose(struct drm_device *dev)
  3610. {
  3611. int ret;
  3612. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3613. return;
  3614. mutex_lock(&dev->struct_mutex);
  3615. ret = i915_gem_idle(dev);
  3616. if (ret)
  3617. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3618. mutex_unlock(&dev->struct_mutex);
  3619. }
  3620. static void
  3621. init_ring_lists(struct intel_ring_buffer *ring)
  3622. {
  3623. INIT_LIST_HEAD(&ring->active_list);
  3624. INIT_LIST_HEAD(&ring->request_list);
  3625. }
  3626. static void i915_init_vm(struct drm_i915_private *dev_priv,
  3627. struct i915_address_space *vm)
  3628. {
  3629. vm->dev = dev_priv->dev;
  3630. INIT_LIST_HEAD(&vm->active_list);
  3631. INIT_LIST_HEAD(&vm->inactive_list);
  3632. INIT_LIST_HEAD(&vm->global_link);
  3633. list_add(&vm->global_link, &dev_priv->vm_list);
  3634. }
  3635. void
  3636. i915_gem_load(struct drm_device *dev)
  3637. {
  3638. drm_i915_private_t *dev_priv = dev->dev_private;
  3639. int i;
  3640. dev_priv->slab =
  3641. kmem_cache_create("i915_gem_object",
  3642. sizeof(struct drm_i915_gem_object), 0,
  3643. SLAB_HWCACHE_ALIGN,
  3644. NULL);
  3645. INIT_LIST_HEAD(&dev_priv->vm_list);
  3646. i915_init_vm(dev_priv, &dev_priv->gtt.base);
  3647. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3648. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3649. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3650. for (i = 0; i < I915_NUM_RINGS; i++)
  3651. init_ring_lists(&dev_priv->ring[i]);
  3652. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3653. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3654. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3655. i915_gem_retire_work_handler);
  3656. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  3657. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3658. if (IS_GEN3(dev)) {
  3659. I915_WRITE(MI_ARB_STATE,
  3660. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  3661. }
  3662. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3663. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3664. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3665. dev_priv->fence_reg_start = 3;
  3666. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  3667. dev_priv->num_fence_regs = 32;
  3668. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3669. dev_priv->num_fence_regs = 16;
  3670. else
  3671. dev_priv->num_fence_regs = 8;
  3672. /* Initialize fence registers to zero */
  3673. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3674. i915_gem_restore_fences(dev);
  3675. i915_gem_detect_bit_6_swizzle(dev);
  3676. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3677. dev_priv->mm.interruptible = true;
  3678. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3679. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3680. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3681. }
  3682. /*
  3683. * Create a physically contiguous memory object for this object
  3684. * e.g. for cursor + overlay regs
  3685. */
  3686. static int i915_gem_init_phys_object(struct drm_device *dev,
  3687. int id, int size, int align)
  3688. {
  3689. drm_i915_private_t *dev_priv = dev->dev_private;
  3690. struct drm_i915_gem_phys_object *phys_obj;
  3691. int ret;
  3692. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3693. return 0;
  3694. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3695. if (!phys_obj)
  3696. return -ENOMEM;
  3697. phys_obj->id = id;
  3698. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3699. if (!phys_obj->handle) {
  3700. ret = -ENOMEM;
  3701. goto kfree_obj;
  3702. }
  3703. #ifdef CONFIG_X86
  3704. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3705. #endif
  3706. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3707. return 0;
  3708. kfree_obj:
  3709. kfree(phys_obj);
  3710. return ret;
  3711. }
  3712. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3713. {
  3714. drm_i915_private_t *dev_priv = dev->dev_private;
  3715. struct drm_i915_gem_phys_object *phys_obj;
  3716. if (!dev_priv->mm.phys_objs[id - 1])
  3717. return;
  3718. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3719. if (phys_obj->cur_obj) {
  3720. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3721. }
  3722. #ifdef CONFIG_X86
  3723. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3724. #endif
  3725. drm_pci_free(dev, phys_obj->handle);
  3726. kfree(phys_obj);
  3727. dev_priv->mm.phys_objs[id - 1] = NULL;
  3728. }
  3729. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3730. {
  3731. int i;
  3732. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3733. i915_gem_free_phys_object(dev, i);
  3734. }
  3735. void i915_gem_detach_phys_object(struct drm_device *dev,
  3736. struct drm_i915_gem_object *obj)
  3737. {
  3738. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3739. char *vaddr;
  3740. int i;
  3741. int page_count;
  3742. if (!obj->phys_obj)
  3743. return;
  3744. vaddr = obj->phys_obj->handle->vaddr;
  3745. page_count = obj->base.size / PAGE_SIZE;
  3746. for (i = 0; i < page_count; i++) {
  3747. struct page *page = shmem_read_mapping_page(mapping, i);
  3748. if (!IS_ERR(page)) {
  3749. char *dst = kmap_atomic(page);
  3750. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3751. kunmap_atomic(dst);
  3752. drm_clflush_pages(&page, 1);
  3753. set_page_dirty(page);
  3754. mark_page_accessed(page);
  3755. page_cache_release(page);
  3756. }
  3757. }
  3758. i915_gem_chipset_flush(dev);
  3759. obj->phys_obj->cur_obj = NULL;
  3760. obj->phys_obj = NULL;
  3761. }
  3762. int
  3763. i915_gem_attach_phys_object(struct drm_device *dev,
  3764. struct drm_i915_gem_object *obj,
  3765. int id,
  3766. int align)
  3767. {
  3768. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3769. drm_i915_private_t *dev_priv = dev->dev_private;
  3770. int ret = 0;
  3771. int page_count;
  3772. int i;
  3773. if (id > I915_MAX_PHYS_OBJECT)
  3774. return -EINVAL;
  3775. if (obj->phys_obj) {
  3776. if (obj->phys_obj->id == id)
  3777. return 0;
  3778. i915_gem_detach_phys_object(dev, obj);
  3779. }
  3780. /* create a new object */
  3781. if (!dev_priv->mm.phys_objs[id - 1]) {
  3782. ret = i915_gem_init_phys_object(dev, id,
  3783. obj->base.size, align);
  3784. if (ret) {
  3785. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3786. id, obj->base.size);
  3787. return ret;
  3788. }
  3789. }
  3790. /* bind to the object */
  3791. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3792. obj->phys_obj->cur_obj = obj;
  3793. page_count = obj->base.size / PAGE_SIZE;
  3794. for (i = 0; i < page_count; i++) {
  3795. struct page *page;
  3796. char *dst, *src;
  3797. page = shmem_read_mapping_page(mapping, i);
  3798. if (IS_ERR(page))
  3799. return PTR_ERR(page);
  3800. src = kmap_atomic(page);
  3801. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3802. memcpy(dst, src, PAGE_SIZE);
  3803. kunmap_atomic(src);
  3804. mark_page_accessed(page);
  3805. page_cache_release(page);
  3806. }
  3807. return 0;
  3808. }
  3809. static int
  3810. i915_gem_phys_pwrite(struct drm_device *dev,
  3811. struct drm_i915_gem_object *obj,
  3812. struct drm_i915_gem_pwrite *args,
  3813. struct drm_file *file_priv)
  3814. {
  3815. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3816. char __user *user_data = to_user_ptr(args->data_ptr);
  3817. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3818. unsigned long unwritten;
  3819. /* The physical object once assigned is fixed for the lifetime
  3820. * of the obj, so we can safely drop the lock and continue
  3821. * to access vaddr.
  3822. */
  3823. mutex_unlock(&dev->struct_mutex);
  3824. unwritten = copy_from_user(vaddr, user_data, args->size);
  3825. mutex_lock(&dev->struct_mutex);
  3826. if (unwritten)
  3827. return -EFAULT;
  3828. }
  3829. i915_gem_chipset_flush(dev);
  3830. return 0;
  3831. }
  3832. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3833. {
  3834. struct drm_i915_file_private *file_priv = file->driver_priv;
  3835. /* Clean up our request list when the client is going away, so that
  3836. * later retire_requests won't dereference our soon-to-be-gone
  3837. * file_priv.
  3838. */
  3839. spin_lock(&file_priv->mm.lock);
  3840. while (!list_empty(&file_priv->mm.request_list)) {
  3841. struct drm_i915_gem_request *request;
  3842. request = list_first_entry(&file_priv->mm.request_list,
  3843. struct drm_i915_gem_request,
  3844. client_list);
  3845. list_del(&request->client_list);
  3846. request->file_priv = NULL;
  3847. }
  3848. spin_unlock(&file_priv->mm.lock);
  3849. }
  3850. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  3851. {
  3852. if (!mutex_is_locked(mutex))
  3853. return false;
  3854. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  3855. return mutex->owner == task;
  3856. #else
  3857. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3858. return false;
  3859. #endif
  3860. }
  3861. static int
  3862. i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
  3863. {
  3864. struct drm_i915_private *dev_priv =
  3865. container_of(shrinker,
  3866. struct drm_i915_private,
  3867. mm.inactive_shrinker);
  3868. struct drm_device *dev = dev_priv->dev;
  3869. struct drm_i915_gem_object *obj;
  3870. int nr_to_scan = sc->nr_to_scan;
  3871. bool unlock = true;
  3872. int cnt;
  3873. if (!mutex_trylock(&dev->struct_mutex)) {
  3874. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  3875. return 0;
  3876. if (dev_priv->mm.shrinker_no_lock_stealing)
  3877. return 0;
  3878. unlock = false;
  3879. }
  3880. if (nr_to_scan) {
  3881. nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
  3882. if (nr_to_scan > 0)
  3883. nr_to_scan -= __i915_gem_shrink(dev_priv, nr_to_scan,
  3884. false);
  3885. if (nr_to_scan > 0)
  3886. i915_gem_shrink_all(dev_priv);
  3887. }
  3888. cnt = 0;
  3889. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
  3890. if (obj->pages_pin_count == 0)
  3891. cnt += obj->base.size >> PAGE_SHIFT;
  3892. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  3893. if (obj->active)
  3894. continue;
  3895. if (obj->pin_count == 0 && obj->pages_pin_count == 0)
  3896. cnt += obj->base.size >> PAGE_SHIFT;
  3897. }
  3898. if (unlock)
  3899. mutex_unlock(&dev->struct_mutex);
  3900. return cnt;
  3901. }
  3902. /* All the new VM stuff */
  3903. unsigned long i915_gem_obj_offset(struct drm_i915_gem_object *o,
  3904. struct i915_address_space *vm)
  3905. {
  3906. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  3907. struct i915_vma *vma;
  3908. if (vm == &dev_priv->mm.aliasing_ppgtt->base)
  3909. vm = &dev_priv->gtt.base;
  3910. BUG_ON(list_empty(&o->vma_list));
  3911. list_for_each_entry(vma, &o->vma_list, vma_link) {
  3912. if (vma->vm == vm)
  3913. return vma->node.start;
  3914. }
  3915. return -1;
  3916. }
  3917. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  3918. struct i915_address_space *vm)
  3919. {
  3920. struct i915_vma *vma;
  3921. list_for_each_entry(vma, &o->vma_list, vma_link)
  3922. if (vma->vm == vm)
  3923. return true;
  3924. return false;
  3925. }
  3926. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  3927. {
  3928. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  3929. struct i915_address_space *vm;
  3930. list_for_each_entry(vm, &dev_priv->vm_list, global_link)
  3931. if (i915_gem_obj_bound(o, vm))
  3932. return true;
  3933. return false;
  3934. }
  3935. unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
  3936. struct i915_address_space *vm)
  3937. {
  3938. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  3939. struct i915_vma *vma;
  3940. if (vm == &dev_priv->mm.aliasing_ppgtt->base)
  3941. vm = &dev_priv->gtt.base;
  3942. BUG_ON(list_empty(&o->vma_list));
  3943. list_for_each_entry(vma, &o->vma_list, vma_link)
  3944. if (vma->vm == vm)
  3945. return vma->node.size;
  3946. return 0;
  3947. }
  3948. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  3949. struct i915_address_space *vm)
  3950. {
  3951. struct i915_vma *vma;
  3952. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3953. if (vma->vm == vm)
  3954. return vma;
  3955. return NULL;
  3956. }